partial void ChooseVideoTapped(UIBarButtonItem sender) { var videoPicker = new UIImagePickerController { ModalPresentationStyle = UIModalPresentationStyle.CurrentContext, SourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum, MediaTypes = new string[] { UTType.Movie } }; videoPicker.FinishedPickingMedia += (object s, UIImagePickerMediaPickedEventArgs e) => { displayLink.Paused = true; playButton.Title = "Play"; popover.Dismiss(true); outputFrames.Clear(); presentationTimes.Clear(); lastCallbackTime = 0.0; var asset = AVAsset.FromUrl(e.MediaUrl); if (assetReader != null && assetReader.Status == AVAssetReaderStatus.Reading) { bufferSemaphore.Release(); assetReader.CancelReading(); } backgroundQueue.DispatchAsync(() => ReadSampleBuffers(asset)); }; videoPicker.Canceled += (object s, EventArgs e) => DismissViewController(true, null); if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) { popover = new UIPopoverController(videoPicker); popover.PresentFromBarButtonItem(sender, UIPopoverArrowDirection.Down, true); } }
private void StartReadingAndWriting() { // Instruct the asset reader and asset writer to get ready to do work if (!_assetReader.StartReading()) { throw new NSErrorException(_assetReader.Error); } if (!_assetWriter.StartWriting()) { throw new NSErrorException(_assetWriter.Error); } // Start a sample-writing session _assetWriter.StartSessionAtSourceTime(_timeRange.Start); // Only set audio handler(obj-c delegate) for audio-only assets, else let the video channel drive progress AVReaderWriter audioHandler = _videoSampleBufferChannel == null ? this : null; var audioTask = StartReadingAsync(_audioSampleBufferChannel, audioHandler); var videoTask = StartReadingAsync(_videoSampleBufferChannel, this); // Set up a callback for when the sample writing is finished Task.WhenAll(audioTask, videoTask).ContinueWith(_ => { if (_cancellationTokenSrc.Token.IsCancellationRequested) { _assetReader.CancelReading(); _assetWriter.CancelWriting(); throw new OperationCanceledException(); } if (_assetReader.Status != AVAssetReaderStatus.Failed) { _assetWriter.FinishWriting(() => { bool success = _assetWriter.Status == AVAssetWriterStatus.Completed; ReadingAndWritingDidFinish(success, _assetWriter.Error); }); } }, _cancellationTokenSrc.Token); }
void StartReadingAndWriting(CMTimeRange timeRange) { // Instruct the asset reader and asset writer to get ready to do work if (!assetReader.StartReading()) { throw new NSErrorException(assetReader.Error); } if (!assetWriter.StartWriting()) { throw new NSErrorException(assetWriter.Error); } // Start a sample-writing session assetWriter.StartSessionAtSourceTime(timeRange.Start); Task audioTask = Start(audioSampleBufferChannel); Task videoTask = Start(videoSampleBufferChannel); // Set up a callback for when the sample writing is finished Task.WhenAll(audioTask, videoTask).ContinueWith(_ => { if (cancellationTokenSrc.Token.IsCancellationRequested) { assetReader.CancelReading(); assetWriter.CancelWriting(); throw new OperationCanceledException(); } if (assetReader.Status != AVAssetReaderStatus.Failed) { assetWriter.FinishWriting(() => { bool success = assetWriter.Status == AVAssetWriterStatus.Completed; ReadingAndWritingDidFinish(success, assetWriter.Error); }); } }, cancellationTokenSrc.Token); }