这个东西相信还是蛮好玩的,有时候我们也确实需要,比如,要将某个MP3转换为WMA文件,或者把MP4转换为WMV文件。
Windows.Media.Transcoding命名空间下提供了一个MediaTranscoder类,这个类就是专门用来转码的,用起来也不算很复杂,但把步骤抽象出来说,意义不大,所以,还是老办法吧。
接下来,我们会完成一个简单的应用,主要功能:打开一个MP3文件,然后将其转为WMA文件输出。
1、做好界面,两个按钮用于操作,一个进度条,显示转换进度,一个TextBlock控件,显示提示信息。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Button Content="打开文件" Click="onOpenfile"/>
<Button Content="开始转换" Click="onStart"/>
</StackPanel>
<ProgressBar x:Name="process" Maximum="100" Minimum="0" Width="350" HorizontalAlignment="Left" Margin="8,21,0,20"/>
<TextBlock x:Name="tbMessage"/>
</StackPanel>
</Grid>
2、后面的处理代码,一是打开文件,二是转码并保存文件。
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.Pickers;
using Windows.Media.Transcoding;
using Windows.Media.MediaProperties;
namespace App1
{
public sealed partial class MainPage : Page
{
/*
musicFile是输入的文件,待转换
wmaOutputFile为输出文件,即转换后的音频文件
*/
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文件", new string[] { ".wma" });
picker.SuggestedStartLocation = PickerLocationId.Desktop;
this.wmaOutputFile = await picker.PickSaveFileAsync();
// 开始转码
if (wmaOutputFile != null)
{
// a,实例化转换器
MediaTranscoder transd = new MediaTranscoder();
// b,预转码测试,返回结果,判断是否能转换
var WmaProfile = MediaEncodingProfile.CreateWma(AudioEncodingQuality.Medium);
PrepareTranscodeResult result = await transd.PrepareFileTranscodeAsync(musicFile, wmaOutputFile, WmaProfile);
if (result.CanTranscode == false)
{
this.tbMessage.Text = "无法转换,原因:" + result.FailureReason.ToString();
return;
}
// c,如果可以转码,就开工
Progress<double> transProgress = new Progress<double>(this.ProcessReport);
this.tbMessage.Text = "正在转码……";
await result.TranscodeAsync().AsTask<double>(transProgress);
this.tbMessage.Text = "转换完成。";
}
}
private async void ProcessReport(double v)
{
// 报告进度
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
this.process.Value = v;
});
}
}
}
a、new一个MediaTranscoder对象;
b、调用MediaTranscoder对象的PrepareFileTranscodeAsync方法,返回一个PrepareTranscodeResult对象;
c、从返回的PrepareTranscodeResult中,通过CanTranscode属性判断是否可以转码,如果可以则为true,如果不行就为false,通过FailureReason属性可以得到不可以转码的原因;
d、调用PrepareTranscodeResult的TranscodeAsync方法开始转码;
e、可以获取其进度反馈,方法就不多说了,上面的代码是一种方法,SDK示例包中还演示了另一种方法,结果是差不多的。
注意,var WmaProfile = MediaEncodingProfile.CreateWma(AudioEncodingQuality.Medium)的时候,AudioEncodingQuality如果使用Auto会发生异常,其他值正常,我也弄不清为什么。
完成后,可以运行测试