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(); }); }
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(); }); }
public async Task Play(Func <Task> ifNotAvailable) { if (Status == PlayerStatus.PAUSED) { Status = PlayerStatus.PLAYING; //We are simply paused so just start again player.Play(); return; } try { // Start off with the status LOADING. Status = PlayerStatus.LOADING; Cover = null; AVAsset nsAsset = AVUrlAsset.FromUrl(nsUrl); AVPlayerItem streamingItem = AVPlayerItem.FromAsset(nsAsset); nsAsset.LoadValuesAsynchronously(new string[] { "commonMetadata" }, delegate { foreach (AVMetadataItem item in streamingItem.Asset.CommonMetadata) { if (item.KeySpace == AVMetadata.KeySpaceID3 && item.CommonKey == AVMetadata.CommonKeyArtwork) { if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0)) { Cover = UIImage.LoadFromData(item.Value as NSData); } else { NSObject data; (item.Value as NSMutableDictionary).TryGetValue(new NSString("data"), out data); Cover = UIImage.LoadFromData(data as NSData); } } } if (Cover == null) { Cover = UIImage.FromFile("placeholder_cover.png"); } }); if (player.CurrentItem != null) { // Remove the observer before destructing the current item player.CurrentItem.RemoveObserver(this, new NSString("status")); } player.ReplaceCurrentItemWithPlayerItem(streamingItem); streamingItem.AddObserver(this, new NSString("status"), NSKeyValueObservingOptions.New, player.Handle); streamingItem.AddObserver(this, new NSString("loadedTimeRanges"), NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New, player.Handle); player.CurrentItem.SeekingWaitsForVideoCompositionRendering = true; player.CurrentItem.AddObserver(this, (NSString)"status", NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial, StatusObservationContext.Handle); NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, async(notification) => { await PlayNext(); }, player.CurrentItem); player.Play(); } catch (Exception ex) { Status = PlayerStatus.STOPPED; //unable to start playback log error Console.WriteLine("Unable to start playback: " + ex); } }