New Fashion Windows 8 Development (34): multimedia file Transcoding

Source: Internet
Author: User

This is a good thing to believe. Sometimes we do need it. For example, we want to convert an MP3 file to a WMA file or a MP4 file to a WMV file.

Windows. media. the transcoding namespace provides a mediatranscoder class, which is specially used for transcoding and is not very complicated to use. However, it is of little significance to abstract the steps, it's still the old method.

 

Next, we will complete a simple application. The main function is to open an MP3 file and convert it into a WMA file for output.

1. Complete the interface. Two buttons are used for operations. One progress bar displays the conversion progress and one textblock control. A prompt is displayed.

 
<Grid background = "{staticresource applicationpagebackgroundthemebrush}"> <stackpanel orientation = "horizontal"> <button content = "Open File" Click = "onopenfile"/> <button content = "Start conversion" Click = "onstart"/> </stackpanel> <progressbar X: name = "process" Maximum = "100" minimum = "0" width = "350" horizontalalignment = "Left" margin = ","/> <textblock X: name = "tbmessage"/> </stackpanel> </GRID>

2. Subsequent processing Code : Opening a file, transcoding, and saving the file.

Using system; using system. collections. generic; using system. io; using system. LINQ; using Windows. foundation; using Windows. foundation. collections; using Windows. UI. XAML; using Windows. UI. XAML. controls; using Windows. UI. XAML. controls. primitives; using Windows. UI. XAML. data; using Windows. UI. XAML. input; using Windows. UI. XAML. media; using Windows. UI. XAML. navigation; using Windows. storage; using Windows. storage. pick ERS; using Windows. media. transcoding; using Windows. media. mediaproperties; namespace app1 {public sealed partial class mainpage: Page {/* musicfile is the input file. The wmaoutputfile to be converted is the output file, that is, the converted audio file */storagefile musicfile = NULL, wmaoutputfile = NULL; Public mainpage () {This. initializecomponent ();} private async void onopenfile (Object sender, routedeventargs e) {fileopenpicker picker = new fileopenpicker (); Picker. filetypefilter. add (". MP3 "); picker. suggestedstartlocation = pickerlocationid. musiclibrary; musicfile = await picker. picksinglefileasync ();} private async void onstart (Object sender, routedeventargs e) {If (this. musicfile = NULL) {return;} filesavepicker picker = new filesavepicker (); picker. filetypechoices. add ("WMA file", new string [] {". WMA "}); picker. suggestedstartlocation = picker Locationid. desktop; this.wma outputfile = await picker. picksavefileasync (); // start transcoding if (wmaoutputfile! = NULL) {// A, instantiation converter mediatranscoder transd = new mediatranscoder (); // B, pre-transcoding test, return results, determine whether to convert var wmaprofile = mediaencodingprofile. createwma (audioencodingquality. medium); preparetranscoderesult result = await transd. preparefiletranscodeasync (musicfile, wmaoutputfile, wmaprofile); If (result. cantranscode = false) {This. tbmessage. TEXT = "cannot be converted, cause:" + result. failurereason. tostring (); return;} // C, If transcoding is available, you can start progress <double> transprogress = new progress <double> (this. processreport); this. tbmessage. Text = "transcoding ...... "; Await result. transcodeasync (). astask <double> (transprogress); this. tbmessage. Text =" the conversion is complete. ";}} Private async void processreport (Double V) {// report progress await dispatcher. runasync (windows. UI. core. coredispatcherpriority. normal, () => {This. process. value = V ;});}}}

A. A new mediatranscoder object;

B. Call the preparefiletranscodeasync METHOD OF THE mediatranscoder object and return a preparetranscoderesult object;

C. From the returned preparetranscoderesult, you can use the cantranscode attribute to determine whether transcoding can be performed. If yes, the value is true. If not, the value is false. You can use the failurereason attribute to obtain the reason why transcoding cannot be performed;

D. Call the transcodeasync method of preparetranscoderesult to start transcoding;

E. You can get the progress feedback. The above code is a method. The SDK sample package also demonstrates another method, and the result is similar.

 

Note: When var wmaprofile = mediaencodingprofile. createwma (audioencodingquality. Medium) is used, audioencodingquality will encounter an exception if auto is used. If other values are normal, I cannot figure out why.

 

You can run the test

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.