Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jdmonty/a0e2289479a293ce0dea to your computer and use it in GitHub Desktop.
Save jdmonty/a0e2289479a293ce0dea to your computer and use it in GitHub Desktop.
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