Created
June 13, 2013 21:20
-
-
Save dannycabrera/5777478 to your computer and use it in GitHub Desktop.
Xamarin.iOS AudioTrim Method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using MonoTouch.Foundation; | |
using MonoTouch.AVFoundation; | |
using MonoTouch.CoreMedia; | |
namespace TestProject.Common | |
{ | |
public class AudioTrim | |
{ | |
AVAsset _asset; | |
AVAssetExportSession _exportSession; | |
public AudioTrim () | |
{ | |
} | |
public void TrimFile(string sourcePath, string exportPath, double stopTime, Action exportCompleted) | |
{ | |
// Configure export | |
_asset = AVAsset.FromUrl (NSUrl.FromFilename (sourcePath)); | |
_exportSession = new AVAssetExportSession ( _asset, AVAssetExportSession.PresetPassthrough); | |
_exportSession.OutputUrl = NSUrl.FromFilename (exportPath); | |
_exportSession.OutputFileType = AVFileType.Aiff; | |
// Set range | |
CMTimeRange range = new CMTimeRange(); | |
range.Start = CMTime.FromSeconds (0, _asset.Duration.TimeScale); | |
range.Duration = CMTime.FromSeconds (stopTime, _asset.Duration.TimeScale); | |
_exportSession.TimeRange = range; | |
// Begin export | |
_exportSession.ExportAsynchronously (new AVCompletionHandler(delegate { | |
switch (_exportSession.Status) { | |
case AVAssetExportSessionStatus.Failed: | |
Console.WriteLine ("Export failed: {0}", _exportSession.Error.Description); | |
break; | |
case AVAssetExportSessionStatus.Cancelled: | |
Console.WriteLine ("Export cancelled"); | |
break; | |
case AVAssetExportSessionStatus.Completed: | |
Console.WriteLine ("Export Completed"); | |
break; | |
default: | |
Console.WriteLine ("Export {0}", _exportSession.Status); | |
break; | |
} | |
exportCompleted(); | |
})); | |
} | |
public void Dispose() | |
{ | |
_asset.Dispose (); | |
_exportSession.Dispose (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Done in response to my post over at the Xamarin.iOS forum. http://forums.xamarin.com/discussion/5090/know-of-any-audio-trimming-examples#latest