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; } }); }
void loadAsset(AVAsset asset, string[] assetKeysToLoad, DispatchGroup dispatchGroup) { dispatchGroup.Enter(); asset.LoadValuesAsynchronously(assetKeysToLoad, () => { foreach (var key in assetKeysToLoad) { NSError error; if (asset.StatusOfValue(key, out error) == AVKeyValueStatus.Failed) { Console.Error.WriteLine("Key value loading failed for key" + key + " with error: " + error.ToString()); dispatchGroup.Leave(); } } if (!asset.Composable) { Console.Error.WriteLine("Asset is not composable"); dispatchGroup.Leave(); } Clips.Add(asset); ClipTimeRanges.Add(NSValue.FromCMTimeRange(new CMTimeRange() { Start = CMTime.FromSeconds(0, 1), Duration = CMTime.FromSeconds(5, 1) })); dispatchGroup.Leave(); }); }
private void LoadAsset(AVAsset asset, string[] assetKeysToLoad, DispatchGroup dispatchGroup) { dispatchGroup.Enter(); asset.LoadValuesAsynchronously(assetKeysToLoad, () => { // First test whether the values of each of the keys we need have been successfully loaded. foreach (var key in assetKeysToLoad) { if (asset.StatusOfValue(key, out NSError error) == AVKeyValueStatus.Failed) { Console.WriteLine($"Key value loading failed for key:{key} with error: {error?.LocalizedDescription ?? ""}"); goto bail; } } if (!asset.Composable) { Console.WriteLine("Asset is not composable"); goto bail; } this.clips.Add(asset); // This code assumes that both assets are atleast 5 seconds long. var value = NSValue.FromCMTimeRange(new CMTimeRange { Start = CMTime.FromSeconds(0, 1), Duration = CMTime.FromSeconds(5, 1) }); this.clipTimeRanges.Add(value); bail: dispatchGroup.Leave(); }); }
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 (); } }
protected override void OnElementChanged(ElementChangedEventArgs <myVideoPlayer> e) { base.OnElementChanged(e); if (e.NewElement != null) { if (Control == null) { // Create AVPlayerViewController _playerViewController = new AVPlayerViewController(); // Set Player property to AVPlayer player = new AVPlayer(); _playerViewController.Player = player; // Use the View from the controller as the native control SetNativeControl(_playerViewController.View); asset = AVAsset.FromUrl(new NSUrl("https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")); if (asset != null) { string[] keys = { "playable", "hasProtectedContent" }; asset.LoadValuesAsynchronously(keys, () => { DispatchQueue.MainQueue.DispatchAsync(() => { // Device.BeginInvokeOnMainThread(() => { if (asset == null) { return; } Console.WriteLine(asset.StatusOfValue("playable", out err)); playerItem = new AVPlayerItem(asset); player.ReplaceCurrentItemWithPlayerItem(playerItem); if (playerItem != null) { player.Play(); } }); }); } } } }
void loadAsset(AVAsset asset, string[] assetKeysToLoad, DispatchGroup dispatchGroup) { dispatchGroup.Enter (); asset.LoadValuesAsynchronously (assetKeysToLoad, () => { foreach(var key in assetKeysToLoad) { NSError error; if(asset.StatusOfValue(key, out error) == AVKeyValueStatus.Failed) { Console.Error.WriteLine("Key value loading failed for key" + key + " with error: "+ error.ToString()); dispatchGroup.Leave(); } } if(!asset.Composable) { Console.Error.WriteLine("Asset is not composable"); dispatchGroup.Leave(); } Clips.Add(asset); ClipTimeRanges.Add(NSValue.FromCMTimeRange(new CMTimeRange(){ Start = CMTime.FromSeconds(0, 1), Duration = CMTime.FromSeconds(5,1) })); dispatchGroup.Leave(); }); }