private void Play(Uri uri) { var nsAsset = AVAsset.FromUrl(DecodeUri(uri)); var streamingItem = AVPlayerItem.FromAsset(nsAsset); _player.ReplaceCurrentItemWithPlayerItem(streamingItem); }
protected virtual void InitializeSource() { PlaybackSession.NaturalDuration = TimeSpan.Zero; PlaybackSession.PositionFromPlayer = TimeSpan.Zero; // Reset player TryDisposePlayer(); if (Source == null) { return; } try { InitializePlayer(); PlaybackSession.PlaybackState = MediaPlaybackState.Opening; _player.CurrentItem?.RemoveObserver(_observer, new NSString("duration"), _player.Handle); _player.CurrentItem?.RemoveObserver(_observer, new NSString("status"), _player.Handle); _player.CurrentItem?.RemoveObserver(_observer, new NSString("loadedTimeRanges"), _player.Handle); if (Source is MediaPlaybackList) { var items = ((MediaPlaybackList)Source).Items; foreach (var item in items) { var asset = AVAsset.FromUrl(DecodeUri(item.Source.Uri)); _player.InsertItem(new AVPlayerItem(asset), null); } } else { var nsAsset = AVAsset.FromUrl(DecodeUri(((MediaSource)Source).Uri)); var streamingItem = AVPlayerItem.FromAsset(nsAsset); _player.ReplaceCurrentItemWithPlayerItem(streamingItem); } _player.CurrentItem.AddObserver(_observer, new NSString("duration"), NSKeyValueObservingOptions.Initial, _player.Handle); _player.CurrentItem.AddObserver(_observer, new NSString("status"), NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial, _player.Handle); _player.CurrentItem.AddObserver(_observer, new NSString("loadedTimeRanges"), NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New, _player.Handle); _player.CurrentItem.SeekingWaitsForVideoCompositionRendering = true; // Adapt pitch to prevent "metallic echo" when changing playback rate _player.CurrentItem.AudioTimePitchAlgorithm = AVAudioTimePitchAlgorithm.TimeDomain; MediaOpened?.Invoke(this, null); } catch (Exception ex) { OnMediaFailed(ex); } }
private void Play() { if (string.IsNullOrEmpty(MediaUrl)) { LoadingSpinner.Hidden = true; return; } //f4v is not supported on iOS (thanks Apple) if (MediaUrl.EndsWith(".f4v", StringComparison.CurrentCultureIgnoreCase)) { //Show alert LoadingSpinner.Hidden = true; _dialog.Show(Translation.alert_display_failed_title, Translation.alert_display_failed_body, Translation.general_close, async() => { await ViewModel?.CloseCommand?.ExecuteAsync(); }); return; } if (MediaUrl.EndsWith(".mp4", StringComparison.CurrentCultureIgnoreCase) || MediaUrl.EndsWith(".m4v", StringComparison.CurrentCultureIgnoreCase)) { ImageView.Hidden = true; NSUrl url = NSUrl.CreateFileUrl(MediaUrl, null); AVPlayerItem item = new AVPlayerItem(url); _avLooper = new AVPlayerLooper(_avplayer, item, CoreMedia.CMTimeRange.InvalidRange); _avplayer.ReplaceCurrentItemWithPlayerItem(item); _avplayer.Play(); } else if (MediaUrl.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase)) { _avplayer.Dispose(); _avplayer = null; _avplayerController.RemoveFromParentViewController(); _avplayerController.View.RemoveFromSuperview(); _avplayerController.Dispose(); _avplayerController = null; UIImage image = UIImage.FromFile(MediaUrl); ImageView.Image = image; } LoadingSpinner.Hidden = true; }