Skip to content

Instantly share code, notes, and snippets.

@dannycabrera
Created June 13, 2013 21:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dannycabrera/5777478 to your computer and use it in GitHub Desktop.
Save dannycabrera/5777478 to your computer and use it in GitHub Desktop.
Xamarin.iOS AudioTrim Method
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 ();
}
}
}
@dannycabrera
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment