我想在我的UWP win 10应用程序中修剪音乐文件(mp3)。我尝试使用Naudio,但它不能在我的应用程序中运行,所以我该怎么做?
任何想法?
答案 0 :(得分:2)
如果要修剪mp3文件,可以使用Windows.Media.Editing namespace,尤其是 MediaClip class 。
默认情况下,此类用于从视频文件剪切。但我们也可以通过在渲染时在 MediaEncodingProfile 方法中设置MediaComposition.RenderToFileAsync来修剪mp3文件。
以下是一个简单的示例:
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
openPicker.FileTypeFilter.Add(".mp3");
var pickedFile = await openPicker.PickSingleFileAsync();
if (pickedFile != null)
{
//Created encoding profile based on the picked file
var encodingProfile = await MediaEncodingProfile.CreateFromFileAsync(pickedFile);
var clip = await MediaClip.CreateFromFileAsync(pickedFile);
// Trim the front and back 25% from the clip
clip.TrimTimeFromStart = new TimeSpan((long)(clip.OriginalDuration.Ticks * 0.25));
clip.TrimTimeFromEnd = new TimeSpan((long)(clip.OriginalDuration.Ticks * 0.25));
var composition = new MediaComposition();
composition.Clips.Add(clip);
var savePicker = new Windows.Storage.Pickers.FileSavePicker();
savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary;
savePicker.FileTypeChoices.Add("MP3 files", new List<string>() { ".mp3" });
savePicker.SuggestedFileName = "TrimmedClip.mp3";
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
//Save to file using original encoding profile
var result = await composition.RenderToFileAsync(file, MediaTrimmingPreference.Precise, encodingProfile);
if (result != Windows.Media.Transcoding.TranscodeFailureReason.None)
{
System.Diagnostics.Debug.WriteLine("Saving was unsuccessful");
}
else
{
System.Diagnostics.Debug.WriteLine("Trimmed clip saved to file");
}
}
}