public void WriteToUrl(NSUrl localOutputURL, Action <float> progress, Action <NSError> completion) { _outputURL = localOutputURL; AVAsset localAsset = _asset; _completionProc = completion; _progressProc = progress; // Dispatch the setup work with _cancellationTokenSrc, to ensure this work can be cancelled localAsset.LoadValuesTaskAsync(new string[] { "tracks", "duration" }).ContinueWith(_ => { // Since we are doing these things asynchronously, the user may have already cancelled on the main thread. // In that case, simply return from this block _cancellationTokenSrc.Token.ThrowIfCancellationRequested(); bool success = true; NSError localError = null; success = localAsset.StatusOfValue("tracks", out localError) == AVKeyValueStatus.Loaded && localAsset.StatusOfValue("duration", out localError) == AVKeyValueStatus.Loaded; if (!success) { throw new NSErrorException(localError); } _timeRange = new CMTimeRange { Start = CMTime.Zero, Duration = localAsset.Duration }; // AVAssetWriter does not overwrite files for us, so remove the destination file if it already exists if (File.Exists(localOutputURL.Path)) { File.Delete(localOutputURL.Path); } // Set up the AVAssetReader and AVAssetWriter, then begin writing samples or flag an error SetupReaderAndWriter(); StartReadingAndWriting(); return(localError); }, _cancellationTokenSrc.Token).ContinueWith(prevTask => { switch (prevTask.Status) { case TaskStatus.Canceled: ReadingAndWritingDidFinish(false, null); break; case TaskStatus.Faulted: ReadingAndWritingDidFinish(false, ((NSErrorException)prevTask.Exception.InnerException).Error); break; default: break; } }); }
public override async void WindowControllerDidLoadNib (NSWindowController windowController) { base.WindowControllerDidLoadNib (windowController); // Add code to here after the controller has loaded the document window var filename = Path.Combine (NSBundle.MainBundle.BundlePath, "sample_iTunes.mov"); NSUrl url = NSUrl.FromFilename ("/Users/kichang/Downloads/sample_iTunes.mov"); asset = AVAsset.FromUrl (url); string[] keys = { "playable", "hasProtectedContent", "tracks", "duration" }; Task task = asset.LoadValuesTaskAsync (keys); await task; NSError assetError; if (asset.StatusOfValue ("playable", out assetError) == AVKeyValueStatus.Failed) { return; } if (asset.Playable) { float height = asset.Tracks [0].NaturalSize.Height; float width = asset.Tracks [0].NaturalSize.Width; playerItem = new AVPlayerItem (asset); player = new AVPlayer (); playerLayer = AVPlayerLayer.FromPlayer (player); playerView.WantsLayer = true; playerView.Layer.BackgroundColor = new CGColor (1, 1, 1, 1); playerLayer.Frame = new RectangleF (0, 0, width, height); playerView.Frame = playerLayer.Frame; playerView.Layer.AddSublayer (playerLayer); player.ReplaceCurrentItemWithPlayerItem (playerItem); player.Play (); } }