public void Play() { if (Source == null || _player == null) { return; } try { if (PlaybackSession.PlaybackState == MediaPlaybackState.None) { // It's AVPlayer default behavior to clear CurrentItem when no next item exists // Solution to this is to reinitialize the source if video was: Ended, Failed or Manually stopped (not paused) // This will also reinitialize all videos in case of source list, but only in one of 3 listed scenarios above InitializeSource(); } PlaybackSession.PlaybackState = MediaPlaybackState.Buffering; _player.Play(); PlaybackSession.PlaybackState = MediaPlaybackState.Playing; } catch (Exception ex) { OnMediaFailed(ex); } }
void InitVideoPlayer() { var path = Path.Combine(NSBundle.MainBundle.BundlePath, Element.Source); if (!NSFileManager.DefaultManager.FileExists(path)) { Console.WriteLine("Video not exist"); _videoPlayer = new AVPlayerViewController(); _videoPlayer.ShowsPlaybackControls = false; _videoPlayer.Player.Volume = 0; _videoPlayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill; _videoPlayer.View.BackgroundColor = UIColor.Clear; SetNativeControl(_videoPlayer.View); return; } _queuePlayer = new AVQueuePlayer(); // Load the video from the app bundle. NSUrl videoUrl = new NSUrl(path, false); // Create and configure the movie player. _videoPlayer = new AVPlayerViewController { View = { Bounds = Bounds, AutoresizingMask = AutoresizingMask }, Player = (Element.Loop)?_queuePlayer: new AVPlayer(videoUrl) }; var playerItem = new AVPlayerItem(videoUrl); _videoPlayer.ShowsPlaybackControls = false; _videoPlayer.Player.Volume = 0; _videoPlayer.VideoGravity = AVLayerVideoGravity.ResizeAspectFill; _looper = new AVPlayerLooper(_queuePlayer, playerItem, CoreMedia.CMTimeRange.InvalidRange); _videoPlayer.View.BackgroundColor = UIColor.Clear; foreach (UIView subView in _videoPlayer.View.Subviews) { subView.BackgroundColor = UIColor.Clear; } if (Element.Loop) { _queuePlayer.Play(); } else { _videoPlayer.Player.Play(); } SetNativeControl(_videoPlayer.View); }
public override void ViewDidLoad() { base.ViewDidLoad(); if (string.IsNullOrEmpty(FilePath) || !File.Exists(FilePath)) { NavigationController.PopViewController(true); } string taskType = Task.TaskType.IdName; if (taskType == "LISTEN_AUDIO" || taskType == "REC_AUDIO" || taskType == "TAKE_VIDEO") { // Load video, play and loop playerItem = AVPlayerItem.FromUrl(NSUrl.FromFilename(FilePath)); player = AVQueuePlayer.FromItems(new AVPlayerItem[] { playerItem }); playerLayer = AVPlayerLayer.FromPlayer(player); playerLooper = AVPlayerLooper.FromPlayer(player, playerItem); if (taskType == "TAKE_VIDEO") { playerLayer.Frame = View.Frame; View.Layer.AddSublayer(playerLayer); } else { ImageView.Hidden = true; AudioIcon.Hidden = false; DescLabel.Text = Task.Description; } player.Play(); } else { // Load image ImageService.Instance.LoadFile(FilePath).Error((e) => { Console.WriteLine("ERROR LOADING IMAGE: " + e.Message); NavigationController.PopViewController(true); }).Into(ImageView); } if (DeleteResult != null) { UIBarButtonItem customButton = new UIBarButtonItem( UIImage.FromFile("ic_delete"), UIBarButtonItemStyle.Plain, (s, e) => { ConfirmDelete(); } ); NavigationItem.RightBarButtonItem = customButton; } }
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; }
public void Play() { if (Source == null || _player == null) { return; } try { // If we reached the end of media, we need to reset position to 0 if (PlaybackSession.PlaybackState == MediaPlaybackState.None) { PlaybackSession.Position = TimeSpan.Zero; } PlaybackSession.PlaybackState = MediaPlaybackState.Buffering; _player.Play(); PlaybackSession.PlaybackState = MediaPlaybackState.Playing; } catch (Exception ex) { OnMediaFailed(ex); } }
private void PlayVideo() { var videoUrl = GetVideoUrl(Element.Source); if (videoUrl == null) { Console.WriteLine($"Video {Element.Source} not found"); return; } var itemToPlay = new AVPlayerItem(AVAsset.FromUrl(videoUrl)); _playerLooper = AVPlayerLooper.FromPlayer(_player, itemToPlay, CMTimeRange.InvalidRange); if (itemToPlay != null) { _player.Play(); } }