Asp.net ..amr .mp3 (using the qiniu conversion method), asp.net. amr
Preface
I believe everyone should know that the. amr format is used for audio of mobile devices. The compression ratio is relatively large, and it is mostly used for voice and calls. The effect is good! Therefore, most mobile devices use the amr format for recording! Common examples: Call recording, voice and recording!
But this ghost is described in two words as "egg pain", because HTML5 does not support it at all (amr: I am an orphan, Director: Zomi Hilla); so, the audition results are hard to achieve! Although you provided an audition, once you leave the page, it means that you will no longer be able to play your recordings!
So you have to convert the file format!
At present, there are two methods to convert the formats mastered by the editor: upload to the qiniu cloud service for conversion and download; and use the ffmpeg application for conversion! This article first introduces how to use qiniu conversion. Let's take a look at it.
Qiniu conversion method:
1. First, register qiniu cloud storage!
2. qiniu will return the "two" passes to you. The pass is an essential tool for communications! One:
AK and SK are prerequisites for program execution!
3. Create a space and add a queue! The space name and queue name are used!
The space is the warehouse that stores the files you uploaded, And the queue is the factory that processes the files! You can create multiple!
Domain name needs to be configured for the space!
4. Download the source code provided by qiniu: I am a link
The file contains a file named "Qiniu.4.0.dll", which is referenced in your project!
/// <Summary> /// set your AK, SK, and domain name /// </summary> private static void SetKey () {// set the AK and SK Config of the account. ACCESS_KEY = "##################################### ###"; config. SECRET_KEY = "************************************* ***"; config. API_HOST = "http ://###. ***. com ";}
5. Upload the file and notify qiniu to convert the file format. The following example shows how to convert the. AMR format to. MP3 format.
/// <Summary> /// upload the file and pre-convert the file format /// </summary> /// <param name = "key"> file name to convert the format </ param> /// <param name = "filePath"> file path </param> /// <returns> </returns> private string upload (string key, string filePath) {IOClient target = new IOClient (); PutExtra extra = new PutExtra (); // set the name of the bucket stored after the file is uploaded. String bucket = "amrtest "; // for normal upload, you only need to set the name of the uploaded space. For the second parameter, you can set the token expiration time PutPolicy put = new PutPolicy (bucket, 3600 );// You can use the saveas parameter to customize the name of the transcoded file. If this parameter is not specified, the file is named by default and saved in the current space. String mp3tpname = key. split ('. ') [0]. toString () + ". mp3 "; String urlbase64 = Qiniu. util. base64URLSafe. encode (bucket + ":" + mp3tpname); // It generally refers to the target Bucket to which the file is to be uploaded ). If it is a "Bucket", // indicates that the Bucket can only be uploaded to (only for new files); if it is a "Bucket: Key", it indicates that a specific file can be modified. Put. Scope = bucket + ":" + key; // optional. If it is not 0, even if the Scope is in the Bucket: Key format, it is also insert only. put. insertOnly = 0; // "|" command to convert the format before the vertical line; command to convert the format after the vertical line, file name and storage space name! Put. persistentOps = "avthumb/mp3/AB/128 k/ar/44100/acodec/libmp 3lame | saveas/" + urlbase64; // The required file must be modified at that "Factory, that is, the queue name! Put. PersistentPipeline = "LittleBai"; // After the audio/video transcoding persistence is complete, the qiniu server sends a processing result notification to the user. The specified url is the interface used to receive notifications. // If 'persistentops 'is set, you must set this field at the same time. persistentpolicyurl = "http ://***. ###. com/***/default. aspx "; // generate upload Token string upToken = put. token (); // upload the file PutRet ret = target. putFile (upToken, key, filePath, extra); // returned data format: {"hash": "FvipQyyMwI0gvGc7_NUd8OVBuJ85", "key": "55456.amr"," persistentId ": "z0.57eb86a945a2652644d64308"} return ret. response. toString ();}
6. Finally, download the converted file from qiniu space to the current server.
/// <Summary> /// download /// </summary> /// <param name = "key"> convert the format file name </param> /// <param name = "savepath"> download path </param> // <returns> </returns> private string download (string key, string savepath) {// construct the download link to be generated for the private space. You can add the name of the file to be downloaded to the domain name of the space. string baseUrl = "http ://***. ###. com/"+ key; // call the MakeRequest method to generate the private download link string private_url = GetPolicy. makeRequest (baseUrl); // download the file HttpWebRequest req = (HttpWebRequest) HttpWebRequest. create (private_url); req. method = "GET"; using (WebResponse wr = req. getResponse () {HttpWebResponse myResponse = (HttpWebResponse) req. getResponse (); string strpath = myResponse. responseUri. toString (); WebClient mywebclient = new WebClient (); try {mywebclient. downloadFile (strpath, savepath);} catch (Exception ex) {savepath = ex. toString () ;}} return savepath ;}
OK. The conversion is complete! Appendix:
Summary
I hope the content in this article will be helpful for your study or work. If you have any questions, please leave a message.