public async Task Pause() { lastPosition = VideoViewCanvas.CurrentPosition; VideoViewCanvas.Pause(); Status = MediaPlayerStatus.Paused; await Task.CompletedTask; }
public void UpdateNotifications(IMediaFile mediaFile, MediaPlayerStatus status) { try { var isPlaying = status == MediaPlayerStatus.Playing || status == MediaPlayerStatus.Buffering; var isPersistent = status == MediaPlayerStatus.Playing || status == MediaPlayerStatus.Buffering || status == MediaPlayerStatus.Paused; var nm = NotificationManagerCompat.From(_applicationContext); if (nm != null && _builder != null) { SetMetadata(mediaFile); AddActionButtons(isPlaying); _builder.SetOngoing(isPersistent); var notification = _builder.Build(); CheckSmallIconInMIUI(notification); nm.Notify(_notificationId, notification); } else { StartNotification(mediaFile, isPlaying, false); } } catch (Exception ex) { Console.WriteLine(ex.Message); StopNotifications(); } }
public void UpdateNotifications(IMediaFile mediaFile, MediaPlayerStatus status) { try { var isPlaying = status == MediaPlayerStatus.Playing || status == MediaPlayerStatus.Buffering; var nm = NotificationManagerCompat.From(_appliactionContext); if (nm != null && _builder != null) { SetMetadata(mediaFile); AddActionButtons(isPlaying); _builder.SetOngoing(isPlaying); nm.Notify(MediaServiceBase.NotificationId, _builder.Build()); } else { StartNotification(mediaFile, isPlaying, false); } } catch (Exception ex) { Console.WriteLine(ex.Message); StopNotifications(); } }
public bool OnError(MediaPlayer mp, MediaError what, int extra) { Stop().Wait(); Status = MediaPlayerStatus.Failed; OnMediaFailed(new MediaFailedEventArgs(what.ToString(), new System.Exception())); return(true); }
private int GetCompatValueByStatus(MediaPlayerStatus state) { switch (state) { case MediaPlayerStatus.Buffering: return(PlaybackStateCompat.StateBuffering); case MediaPlayerStatus.Failed: return(PlaybackStateCompat.StateError); case MediaPlayerStatus.Loading: return(PlaybackStateCompat.StateSkippingToQueueItem); case MediaPlayerStatus.Paused: return(PlaybackStateCompat.StatePaused); case MediaPlayerStatus.Playing: return(PlaybackStateCompat.StatePlaying); case MediaPlayerStatus.Stopped: return(PlaybackStateCompat.StateStopped); default: return(PlaybackStateCompat.StateNone); } }
public void OnPrepared(MediaPlayer mp) { if (Status == MediaPlayerStatus.Buffering) { VideoViewCanvas.Start(); } Status = MediaPlayerStatus.Playing; }
private void ObserveRate() { var stoppedPlaying = Rate == 0.0; if (stoppedPlaying && Status == MediaPlayerStatus.Playing) { //Update the status becuase the system changed the rate. Status = MediaPlayerStatus.Paused; } }
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; }
public void OnPrepared(MediaPlayer mp) { Console.WriteLine($"OnPrepared: {Status}"); _mediaPlayer = mp; if (Status == MediaPlayerStatus.Buffering) { VideoViewCanvas.Start(); } Status = MediaPlayerStatus.Playing; }
private void UpdateStatus(MediaPlayerStatus status) { var item = new TutorialStatus() { Id = string.Empty, LastAction = Enum.GetName(typeof(MediaPlayerStatus), status), TutorialName = "big buck bunny", LastPayedTime = FixTimeSpan(CrossMediaManager.Current.VideoPlayer.Position).ToString(@"hh\:mm\:ss") }; var service = Locator.Instance.Resolve <ITutorialsService>(); service.SaveVideoStatus(item); }
public VideoPlayerImplementation() { _status = MediaPlayerStatus.Stopped; // Watch the buffering status. If it changes, we may have to resume because the playing stopped because of bad network-conditions. BufferingChanged += (sender, e) => { // If the player is ready to play, it's paused and the status is still on PLAYING, go on! if ((Player.Status == AVPlayerStatus.ReadyToPlay) && (Rate == 0.0f) && (Status == MediaPlayerStatus.Playing)) Player.Play(); }; }
private void ObserveStatus() { Console.WriteLine("Status Observed Method {0}", Player.Status); if ((Player.Status == AVPlayerStatus.ReadyToPlay) && (Status == MediaPlayerStatus.Buffering)) { Status = MediaPlayerStatus.Playing; Player.Play(); } else if (Player.Status == AVPlayerStatus.Failed) { OnMediaFailed(); Status = MediaPlayerStatus.Stopped; } }
public async Task Play(IMediaFile mediaFile = null) { if (!IsReadyRendering) { //Android ViewRenderer might not initialize Control yet return; } if (isPlayerReady == false) { //await Task.Delay(100); Init(); isPlayerReady = true; } if (mediaFile == null || (mediaFile != null && string.IsNullOrEmpty(mediaFile.Url))) { return; } if (mediaFile != null && CurrentFile != mediaFile) { CurrentFile = mediaFile; currentUri = Android.Net.Uri.Parse(mediaFile.Url); VideoViewCanvas.StopPlayback(); //VideoViewCanvas.Suspend(); Status = MediaPlayerStatus.Stopped; } if (Status == MediaPlayerStatus.Paused) { //We are simply paused so just continue VideoViewCanvas.SeekTo(lastPosition); VideoViewCanvas.Start(); Status = MediaPlayerStatus.Playing; return; } try { Status = MediaPlayerStatus.Buffering; VideoViewCanvas.SetVideoURI(currentUri, RequestHeaders); } catch (System.Exception ex) { OnMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); Status = MediaPlayerStatus.Stopped; } }
public AudioPlayerImplementation() { _status = MediaPlayerStatus.Stopped; // Watch the buffering status. If it changes, we may have to resume because the playing stopped because of bad network-conditions. BufferingChanged += (sender, e) => { // If the player is ready to play, it's paused and the status is still on PLAYING, go on! if ((Player.Status == AVPlayerStatus.ReadyToPlay) && (Rate == 0.0f) && (Status == MediaPlayerStatus.Playing)) { Player.Play(); } }; }
public async Task PlayPause_NotPlaying_Pauses(MediaPlayerStatus notPlayingStatus) { _mediaManagerMock .Setup(mediaManager => mediaManager.Play((IMediaFile)null)) .Returns(Task.FromResult(0)); MediaManagerStatus = notPlayingStatus; var playbackController = new PlaybackController(MediaManager); await playbackController.PlayPause(); _mediaManagerMock .Verify(mediaManager => mediaManager.Play((IMediaFile)null), Times.Once); }
public void Init() { Status = MediaPlayerStatus.Loading; if (UseNativeControls) { var mediaController = new MediaController(((VideoView)RenderSurface).Context); mediaController.SetAnchorView(VideoViewCanvas); VideoViewCanvas.SetMediaController(mediaController); } VideoViewCanvas.SetOnCompletionListener(this); VideoViewCanvas.SetOnErrorListener(this); VideoViewCanvas.SetOnPreparedListener(this); VideoViewCanvas.SetOnInfoListener(this); }
private void ObserveStatus() { Console.WriteLine("Status Observed Method {0}", CurrentItem.Status); var isBuffering = Status == MediaPlayerStatus.Buffering; if (CurrentItem.Status == AVPlayerItemStatus.ReadyToPlay && isBuffering) { Status = MediaPlayerStatus.Playing; Player.Play(); } else if (CurrentItem.Status == AVPlayerItemStatus.Failed) { OnMediaFailed(); Status = MediaPlayerStatus.Stopped; } }
public AudioPlayerImplementation(IVolumeManager volumeManager) { _volumeManager = volumeManager; _status = MediaPlayerStatus.Stopped; // Watch the buffering status. If it changes, we may have to resume because the playing stopped because of bad network-conditions. BufferingChanged += (sender, e) => { // If the player is ready to play, it's paused and the status is still on PLAYING, go on! if ((Player.Status == AVPlayerStatus.ReadyToPlay) && (Rate == 0.0f) && (Status == MediaPlayerStatus.Playing)) Player.Play(); }; _volumeManager.Mute = _player.Muted; _volumeManager.CurrentVolume = _player.Volume; _volumeManager.MaxVolume = 1; _volumeManager.VolumeChanged += VolumeManagerOnVolumeChanged; }
void SendStatus() { if (hub == null || hub.State != HubConnectionState.Connected) { return; } MediaPlayerStatus status = new MediaPlayerStatus() { Hostname = Environment.MachineName, LocalDateTime = DateTime.Now, MediaFiles = mediaPlayer.GetPlayingMediaStatus() }; var json = JsonConvert.SerializeObject(status); hub.InvokeAsync("StatusUpdate", json); }
public AudioPlayerImplementation(IVolumeManager volumeManager) { _volumeManager = volumeManager; _versionHelper = new VersionHelper(); _status = MediaPlayerStatus.Stopped; InitializePlayer(); // Watch the buffering status. If it changes, we may have to resume because the playing stopped because of bad network-conditions. BufferingChanged += (sender, e) => { // If the player is ready to play, it's paused and the status is still on PLAYING, go on! var isPlaying = Status == MediaPlayerStatus.Playing; if (CurrentItem.Status == AVPlayerItemStatus.ReadyToPlay && Rate == 0.0f && isPlaying) { Player.Play(); } }; }
public VideoPlayerImplementation(IVolumeManager volumeManager) { _volumeManager = volumeManager; _status = MediaPlayerStatus.Stopped; // Watch the buffering status. If it changes, we may have to resume because the playing stopped because of bad network-conditions. BufferingChanged += (sender, e) => { // If the player is ready to play, it's paused and the status is still on PLAYING, go on! if ((Player.Status == AVPlayerStatus.ReadyToPlay) && (Rate == 0.0f) && (Status == MediaPlayerStatus.Playing)) { Player.Play(); } }; _volumeManager.Mute = Player.Muted; _volumeManager.CurrentVolume = Player.Volume; _volumeManager.MaxVolume = 1; _volumeManager.VolumeChanged += VolumeManagerOnVolumeChanged; }
public void UpdateNotifications(IMediaFile mediaFile, MediaPlayerStatus status) { try { var isPlaying = status == MediaPlayerStatus.Playing || status == MediaPlayerStatus.Buffering; var nm = NotificationManagerCompat.From(_appliactionContext); if (nm != null && _builder != null) { SetMetadata(mediaFile); AddActionButtons(isPlaying); nm.Notify(MediaServiceBase.NotificationId, _builder.Build()); } else { StartNotification(mediaFile, isPlaying, false); } } catch (Exception ex) { Console.WriteLine(ex.Message); StopNotifications(); } }
public async Task Play(IMediaFile mediaFile = null) { if (VideoViewCanvas == null) { await Task.Delay(100); Init(); } if (mediaFile != null) { CurrentFile = mediaFile; currentUri = Android.Net.Uri.Parse(mediaFile.Url); } if (Status == MediaPlayerStatus.Paused) { Status = MediaPlayerStatus.Playing; //We are simply paused so just continue VideoViewCanvas.Resume(); return; } try { Status = MediaPlayerStatus.Buffering; VideoViewCanvas.SetVideoURI(currentUri); VideoViewCanvas.Start(); Status = MediaPlayerStatus.Playing; } catch (Exception ex) { MediaFailed?.Invoke(this, new MediaFailedEventArgs(ex.Message, ex)); Status = MediaPlayerStatus.Stopped; } }
public void UpdateNotifications(IMediaFile mediaFile, MediaPlayerStatus status) { }
private int GetCompatValueByStatus(MediaPlayerStatus state) { switch (state) { case MediaPlayerStatus.Buffering: return PlaybackStateCompat.StateBuffering; case MediaPlayerStatus.Failed: return PlaybackStateCompat.StateError; case MediaPlayerStatus.Loading: return PlaybackStateCompat.StateSkippingToQueueItem; case MediaPlayerStatus.Paused: return PlaybackStateCompat.StatePaused; case MediaPlayerStatus.Playing: return PlaybackStateCompat.StatePlaying; case MediaPlayerStatus.Stopped: return PlaybackStateCompat.StateStopped; default: return PlaybackStateCompat.StateNone; } }
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) => { }; }
public async Task Pause() { VideoViewCanvas.Pause(); Status = MediaPlayerStatus.Paused; await Task.CompletedTask; }
public async Task Play(IMediaFile mediaFile = null) { if (VideoViewCanvas == null) { await Task.Delay(100); Init(); } if (mediaFile != null) { CurrentFile = mediaFile; currentUri = Android.Net.Uri.Parse(mediaFile.Url); } if (Status == MediaPlayerStatus.Paused) { Status = MediaPlayerStatus.Playing; //We are simply paused so just continue VideoViewCanvas.Resume(); return; } try { Status = MediaPlayerStatus.Buffering; VideoViewCanvas.SetVideoURI(currentUri); VideoViewCanvas.Start(); Status = MediaPlayerStatus.Playing; } catch(Exception ex) { MediaFailed?.Invoke(this, new MediaFailedEventArgs(ex.Message, ex)); Status = MediaPlayerStatus.Stopped; } }
public async Task Stop() { VideoViewCanvas.StopPlayback(); Status = MediaPlayerStatus.Stopped; await Task.CompletedTask; }
public bool OnError(MediaPlayer mp, MediaError what, int extra) { Stop().Wait(); Status = MediaPlayerStatus.Failed; OnMediaFailed(new MediaFailedEventArgs(what.ToString(), new System.Exception())); return true; }
public void OnPrepared(MediaPlayer mp) { if(Status == MediaPlayerStatus.Buffering) VideoViewCanvas.Start(); Status = MediaPlayerStatus.Playing; }
public void UpdateNotifications(IMediaFile mediaFile, MediaPlayerStatus status) { UpdateInfoFromMediaFile(mediaFile); }
public StatusChangedEventArgs(MediaPlayerStatus status) { Status = status; }
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 nsAsset = AVAsset.FromUrl(nsUrl); 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); } }
public override void UpdateNotifications(IMediaFile mediaFile, MediaPlayerStatus status) { TrySetNowPlayingInfo(mediaFile); base.UpdateNotifications(mediaFile, status); }
public async Task Play(IMediaFile mediaFile = null) { if (VideoViewCanvas == null) throw new System.Exception("No canvas set for video to play in"); if (isPlayerReady == false) { //await Task.Delay(100); Init(); isPlayerReady = true; } if (mediaFile == null || (mediaFile != null && string.IsNullOrEmpty(mediaFile.Url))) { return; } if (mediaFile != null && CurrentFile != mediaFile) { CurrentFile = mediaFile; currentUri = Android.Net.Uri.Parse(mediaFile.Url); VideoViewCanvas.StopPlayback(); //VideoViewCanvas.Suspend(); Status = MediaPlayerStatus.Stopped; } if (Status == MediaPlayerStatus.Paused) { //We are simply paused so just continue VideoViewCanvas.SeekTo(lastPosition); VideoViewCanvas.Start(); Status = MediaPlayerStatus.Playing; return; } try { Status = MediaPlayerStatus.Buffering; VideoViewCanvas.SetVideoURI(currentUri, RequestHeaders); } catch(System.Exception ex) { OnMediaFailed(new MediaFailedEventArgs(ex.Message, ex)); Status = MediaPlayerStatus.Stopped; } }
private void OnStatusChanged(MediaPlayerStatus status) { var handler = this.StatusChanged; if (handler != null) handler(this, new StatusEventArgs(status)); }
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.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) => { }; _player.MediaOpened += (sender, args) => { _loadMediaTaskCompletionSource.SetResult(true); }; _volumeManager.CurrentVolume = (float)_player.Volume; _volumeManager.Mute = _player.IsMuted; _volumeManager.VolumeChanged += VolumeManagerOnVolumeChanged; }
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; }