private async void OnMediaFinished(object sender, MediaFinishedEventArgs e)
 {
     if (sender != CurrentPlaybackManager)
     {
         return;
     }
     MediaFinished?.Invoke(sender, e);
     await PlayNext();
 }
예제 #2
0
        public async Task Play(IMediaFile mediaFile = null)
        {
            var sameMediaFile = mediaFile == null || mediaFile.Equals(_currentMediaFile);

            if (Status == MediaPlayerStatus.Paused && sameMediaFile)
            {
                Status = MediaPlayerStatus.Playing;
                //We are simply paused so just start again
                Player.Play();
                return;
            }

            if (mediaFile != null)
            {
                nsUrl             = MediaFileUrlHelper.GetUrlFor(mediaFile);
                _currentMediaFile = mediaFile;
            }

            try
            {
                InitializePlayer();

                Status = MediaPlayerStatus.Buffering;

                var playerItem = GetPlayerItem(nsUrl);

                CurrentItem?.RemoveObserver(this, new NSString("status"));

                Player.ReplaceCurrentItemWithPlayerItem(playerItem);
                CurrentItem.AddObserver(this, new NSString("loadedTimeRanges"),
                                        NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New,
                                        LoadedTimeRangesObservationContext.Handle);

                CurrentItem.SeekingWaitsForVideoCompositionRendering = true;
                CurrentItem.AddObserver(this, (NSString)"status", NSKeyValueObservingOptions.New |
                                        NSKeyValueObservingOptions.Initial,
                                        StatusObservationContext.Handle);


                NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification,
                                                               notification => MediaFinished?.Invoke(this, new MediaFinishedEventArgs(mediaFile)), CurrentItem);

                Player.Play();
            }
            catch (Exception ex)
            {
                OnMediaFailed();
                Status = MediaPlayerStatus.Stopped;

                //unable to start playback log error
                Console.WriteLine("Unable to start playback: " + ex);
            }

            await Task.CompletedTask;
        }
        public async Task Play(IMediaFile mediaFile = null)
        {
            if (mediaFile != null)
            {
                nsUrl = new NSUrl(mediaFile.Url);
            }

            if (Status == MediaPlayerStatus.Paused)
            {
                Status = MediaPlayerStatus.Playing;
                //We are simply paused so just start again
                Player.Play();
                return;
            }

            try
            {
                // Start off with the status LOADING.
                Status = MediaPlayerStatus.Buffering;

                var options = MediaFileUrlHelper.GetOptionsWithHeaders(RequestHeaders);

                var nsAsset       = AVUrlAsset.Create(nsUrl, options);
                var streamingItem = AVPlayerItem.FromAsset(nsAsset);

                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,
                                                               notification => MediaFinished?.Invoke(this, new MediaFinishedEventArgs(mediaFile)), Player.CurrentItem);

                Player.Play();
            }
            catch (Exception ex)
            {
                OnMediaFailed();
                Status = MediaPlayerStatus.Stopped;

                //unable to start playback log error
                Console.WriteLine("Unable to start playback: " + ex);
            }

            await Task.CompletedTask;
        }
 private async void OnMediaFinished(object sender, MediaFinishedEventArgs e)
 {
     if (sender != CurrentPlaybackManager)
     {
         return;
     }
     MediaFinished?.Invoke(sender, e);
     if (MediaQueue.Repeat == RepeatType.RepeatOne)
     {
         await Seek(TimeSpan.Zero);
     }
     else
     {
         await PlayNext();
     }
 }
예제 #5
0
        internal void OnServiceConnected(MediaServiceBinder serviceBinder)
        {
            Binder  = serviceBinder;
            isBound = true;

            if (AlternateRemoteCallback != null)
            {
                GetMediaPlayerService().AlternateRemoteCallback = AlternateRemoteCallback;
            }

            //serviceGetMediaPlayerService().CoverReloaded += (object sender, EventArgs e) => { instance.CoverReloaded?.Invoke(sender, e); };
            GetMediaPlayerService().StatusChanged    += (object sender, StatusChangedEventArgs e) => { StatusChanged?.Invoke(this, e); };
            GetMediaPlayerService().PlayingChanged   += (sender, args) => { PlayingChanged?.Invoke(this, args); };
            GetMediaPlayerService().BufferingChanged += (sender, args) => { BufferingChanged?.Invoke(this, args); };
            GetMediaPlayerService().MediaFinished    += (sender, args) => { MediaFinished?.Invoke(this, args); };
            GetMediaPlayerService().MediaFileFailed  += (sender, args) => { MediaFileFailed?.Invoke(this, args); };
            GetMediaPlayerService().MediaFailed      += (sender, args) => { MediaFailed?.Invoke(this, args); };
            GetMediaPlayerService().SetMediaSession(_sessionManager);
        }
        public AudioPlayerImplementation()
        {
            _player            = new MediaPlayer();
            _playProgressTimer = new Timer(state =>
            {
                if (_player.PlaybackSession.PlaybackState == MediaPlaybackState.Playing)
                {
                    var progress = _player.PlaybackSession.Position.TotalSeconds /
                                   _player.PlaybackSession.NaturalDuration.TotalSeconds;
                    if (double.IsNaN(progress))
                    {
                        progress = 0;
                    }
                    PlayingChanged?.Invoke(this, new PlayingChangedEventArgs(progress, _player.PlaybackSession.Position, _player.PlaybackSession.NaturalDuration));
                }
            }, null, 0, int.MaxValue);

            _player.MediaFailed += (sender, args) =>
            {
                _status = MediaPlayerStatus.Failed;
                _playProgressTimer.Change(0, int.MaxValue);
                MediaFailed?.Invoke(this, new MediaFailedEventArgs(args.ErrorMessage, args.ExtendedErrorCode));
            };

            _player.PlaybackSession.PlaybackStateChanged += (sender, args) =>
            {
                switch (sender.PlaybackState)
                {
                case MediaPlaybackState.None:
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                case MediaPlaybackState.Opening:
                    Status = MediaPlayerStatus.Loading;
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                case MediaPlaybackState.Buffering:
                    Status = MediaPlayerStatus.Buffering;
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                case MediaPlaybackState.Playing:
                    if (sender.PlaybackRate <= 0 && sender.Position == TimeSpan.Zero)
                    {
                        Status = MediaPlayerStatus.Stopped;
                    }
                    else
                    {
                        Status = MediaPlayerStatus.Playing;
                        _playProgressTimer.Change(0, 50);
                    }
                    break;

                case MediaPlaybackState.Paused:
                    Status = MediaPlayerStatus.Paused;
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            };

            _player.MediaEnded += (sender, args) => { MediaFinished?.Invoke(this, new MediaFinishedEventArgs(_currentMediaFile)); };
            _player.PlaybackSession.BufferingProgressChanged += (sender, args) =>
            {
                var bufferedTime =
                    TimeSpan.FromSeconds(_player.PlaybackSession.BufferingProgress *
                                         _player.PlaybackSession.NaturalDuration.TotalSeconds);
                BufferingChanged?.Invoke(this,
                                         new BufferingChangedEventArgs(_player.PlaybackSession.BufferingProgress, bufferedTime));
            };

            _player.PlaybackSession.SeekCompleted += (sender, args) => { };
        }
 protected virtual void OnMediaFinished(MediaFinishedEventArgs e)
 {
     MediaFinished?.Invoke(this, e);
 }
예제 #8
0
 public void OnCompletion(MediaPlayer mp)
 {
     MediaFinished?.Invoke(this, new MediaFinishedEventArgs(CurrentFile));
 }
예제 #9
0
 private void _vlcMediaPlayer_EndReached(object sender, VlcMediaPlayerEndReachedEventArgs e)
 {
     MediaFinished?.Invoke();
 }
        public VideoPlayerImplementation(IVolumeManager volumeManager)
        {
            _volumeManager = volumeManager;
            _player        = new MediaPlayer();

            _playProgressTimer = new Timer(state =>
            {
                if (_player.PlaybackSession.PlaybackState == MediaPlaybackState.Playing)
                {
                    var progress = _player.PlaybackSession.Position.TotalSeconds /
                                   _player.PlaybackSession.NaturalDuration.TotalSeconds;
                    if (double.IsInfinity(progress))
                    {
                        progress = 0;
                    }
                    PlayingChanged?.Invoke(this, new PlayingChangedEventArgs(progress, _player.PlaybackSession.Position, _player.PlaybackSession.NaturalDuration));
                }
            }, null, 0, int.MaxValue);

            _player.MediaFailed += (sender, args) =>
            {
                _status = MediaPlayerStatus.Failed;
                _playProgressTimer.Change(0, int.MaxValue);
                MediaFailed?.Invoke(this, new MediaFailedEventArgs(args.ErrorMessage, args.ExtendedErrorCode));
            };

            _player.PlaybackSession.PlaybackStateChanged += (sender, args) =>
            {
                switch (sender.PlaybackState)
                {
                case MediaPlaybackState.None:
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                case MediaPlaybackState.Opening:
                    Status = MediaPlayerStatus.Loading;
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                case MediaPlaybackState.Buffering:
                    Status = MediaPlayerStatus.Buffering;
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                case MediaPlaybackState.Playing:
                    if ((sender.PlaybackRate <= 0) && (sender.Position == TimeSpan.Zero))
                    {
                        Status = MediaPlayerStatus.Stopped;
                    }
                    else
                    {
                        Status = MediaPlayerStatus.Playing;
                        _playProgressTimer.Change(0, 50);
                    }
                    break;

                case MediaPlaybackState.Paused:
                    Status = MediaPlayerStatus.Paused;
                    _playProgressTimer.Change(0, int.MaxValue);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            };

            _player.MediaEnded += (sender, args) => { MediaFinished?.Invoke(this, new MediaFinishedEventArgs(_currentMediaFile)); };
            _player.PlaybackSession.BufferingProgressChanged += (sender, args) =>
            {
                var bufferedTime =
                    TimeSpan.FromSeconds(_player.PlaybackSession.BufferingProgress *
                                         _player.PlaybackSession.NaturalDuration.TotalSeconds);
                BufferingChanged?.Invoke(this,
                                         new BufferingChangedEventArgs(_player.PlaybackSession.BufferingProgress, bufferedTime));
            };

            _player.PlaybackSession.SeekCompleted += (sender, args) => { };
            _player.MediaOpened          += (sender, args) => { _loadMediaTaskCompletionSource.SetResult(true); };
            _volumeManager.CurrentVolume  = (float)_player.Volume;
            _volumeManager.Mute           = _player.IsMuted;
            _volumeManager.VolumeChanged += VolumeManagerOnVolumeChanged;
        }