public void PlayPrepare(string fileName) { try { var filePath = _Storage.GetPath(fileName); var url = NSUrl.FromFilename(filePath); if (_Player == null) { _Player = new AVPlayer(); } if (_PlayerItem != null) { _Player.RemoveTimeObserver(_TimeChanged); // for skipping 1-2 extra triggering for new item creation code below CleanPlayerItem(); } PlayerReady = false; _Asset = AVAsset.FromUrl(url); _PlayerItem = new AVPlayerItem(_Asset); _PlayerItem.AddObserver(this, _status, NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New, _Player.Handle); _PlayerItem.AddObserver(this, _error, NSKeyValueObservingOptions.Initial | NSKeyValueObservingOptions.New, _Player.Handle); _Player.ReplaceCurrentItemWithPlayerItem(_PlayerItem); _Player.Rate = 1; _Player.Pause(); // HACK: stop autoplaying after recording _TimeChanged = _Player.AddPeriodicTimeObserver(new CMTime(1, 1), DispatchQueue.MainQueue, time => { // skip restart event if (_isRestart) { _isRestart = false; return; } if (_PlayerItem != null && _PlayerItem.Duration != CMTime.Indefinite) { var stopped = time == _PlayerItem.Duration; PlaybackStatus?.Invoke(this, new PlaybackEventArgs { CurrentTime = time.Seconds, Duration = _PlayerItem.Duration.Seconds, Stopped = stopped }); if (stopped) { _isRestart = true; _Player.Seek(CMTime.Zero); } } }); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(string.Format("*** AudioService.PlayPrepare - Exception: {0}", ex)); } }
private void PlaybackMediaEnded(MediaPlayer sender, object args) { _playbackTimer.Stop(); PlaybackStatus?.Invoke(this, new PlaybackEventArgs { Stopped = true, CurrentTime = sender.PlaybackSession.NaturalDuration.TotalSeconds, Duration = sender.PlaybackSession.NaturalDuration.TotalSeconds }); }
private void PlaybackMediaOpened(MediaPlayer sender, object args) { PlayerReady = true; PlaybackStatus?.Invoke(this, new PlaybackEventArgs { Stopped = true, CurrentTime = 0, Duration = sender.PlaybackSession.NaturalDuration.TotalSeconds, IsNew = true }); PlayerStatus?.Invoke(this, new EventArgs()); }
private void PlaybackMediaFailed(MediaPlayer sender, MediaPlayerFailedEventArgs args) { _playbackTimer.Stop(); PlayerReady = false; PlaybackStatus?.Invoke(this, new PlaybackEventArgs { Stopped = true, Duration = 0, CurrentTime = 0, HasError = true }); }
private void PlaybackStateChanged(MediaPlayer sender, object args) { if (!PlayerReady) { return; } PlaybackStatus.Invoke(this, new PlaybackEventArgs { Stopped = sender.PlaybackSession.PlaybackState != MediaPlaybackState.Playing && sender.PlaybackSession.PlaybackState != MediaPlaybackState.Paused, CurrentTime = sender.PlaybackSession.Position.TotalSeconds, Duration = sender.PlaybackSession.NaturalDuration.TotalSeconds }); }
public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) { var path = keyPath.ToString(); System.Diagnostics.Debug.WriteLine(string.Format("*** AudioService.ObserveValue - Value change: {0} = {1}", keyPath, change.ToString())); if (path.Equals(_status)) { if (_Player.Status == AVPlayerStatus.ReadyToPlay) { if (!PlayerReady) { PlayerReady = _PlayerItem.Duration != CMTime.Indefinite; if (PlayerReady) { PlaybackStatus?.Invoke(this, new PlaybackEventArgs { CurrentTime = 0, Duration = _PlayerItem.Duration.Seconds, Stopped = true, IsNew = true }); PlayerStatus?.Invoke(this, new EventArgs()); } } } } else if (path.Equals(_error)) { if (_Player.Error != null) { PlayerReady = false; PlaybackStatus?.Invoke(this, new PlaybackEventArgs { CurrentTime = 0, Duration = 0, Stopped = true, HasError = true }); PlayerStatus?.Invoke(this, new EventArgs()); } } }
protected virtual void OnPlaybackStatus(PlaybackEngine.PlaybackStatusEventArgs e) => PlaybackStatus?.Invoke(this, e);