2016-04-29 59 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