2016-04-29 31 views
0

我想在我的UWP win 10应用程序中修剪音乐文件(mp3)。我尝试使用Naudio,但它不适用于我的应用程序,所以我该怎么做?我如何在UWP中修剪mp3文件

任何任何想法?

回答

1

如果你想修剪一个mp3文件,你可以使用Windows.Media.Editing namespace,特别是MediaClip class

默认情况下,此类用于剪辑视频文件。但是我们也可以通过设置MediaEncodingProfileMediaComposition.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"); 
     } 
    } 
} 
+0

韩国社交协会非常感谢!你节省了我的时间,我花了一天的时间!非常感谢! – Thanhtu150