private void OnPlaying() { var progress = (Position.TotalSeconds / Duration.TotalSeconds) * 100; var position = Position; var duration = Duration; if (Status == MediaPlayerStatus.Playing || Status == MediaPlayerStatus.Buffering) { Task.Delay(500).ContinueWith(task => OnPlaying(), _onPlayingCancellationSource.Token); } if (Status == MediaPlayerStatus.Stopped || Status == MediaPlayerStatus.Failed) { duration = TimeSpan.Zero; position = TimeSpan.Zero; progress = 0; } PlayingChanged?.Invoke(this, new PlayingChangedEventArgs( progress >= 0 ? progress : 0, position.TotalSeconds >= 0 ? position : TimeSpan.Zero, duration.TotalSeconds >= 0 ? duration : TimeSpan.Zero)); }
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 OnPlayingChanged(PlayingChangedEventArgs e) { PlayingChanged?.Invoke(this, e); }
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; }