private void InitializePlayer() { _player = new AVPlayer(); _videoLayer = AVPlayerLayer.FromPlayer(_player); var avSession = AVAudioSession.SharedInstance(); // By setting the Audio Session category to AVAudioSessionCategorPlayback, audio will continue to play when the silent switch is enabled, or when the screen is locked. avSession.SetCategory(AVAudioSessionCategory.Playback); NSError activationError = null; avSession.SetActive(true, out activationError); if (activationError != null) { Console.WriteLine("Could not activate audio session {0}", activationError.LocalizedDescription); } Player.AddPeriodicTimeObserver(new CMTime(1, 4), DispatchQueue.MainQueue, delegate { var totalDuration = TimeSpan.FromSeconds(_player.CurrentItem.Duration.Seconds); var totalProgress = Position.TotalMilliseconds / totalDuration.TotalMilliseconds; PlayingChanged?.Invoke(this, new PlayingChangedEventArgs(totalProgress, Position, Duration)); }); }
private void OnPlayingChanged(object sender, PlayingChangedEventArgs e) { if (sender == CurrentPlaybackManager) { PlayingChanged?.Invoke(sender, e); } }
private void InitializePlayer() { if (_player != null) { _player.RemoveTimeObserver(PeriodicTimeObserverObject); _player.RemoveObserver(this, (NSString)"rate", RateObservationContext.Handle); _player.Dispose(); } _player = new AVPlayer(); if (_versionHelper.SupportsAutomaticWaitPlayerProperty) { _player.AutomaticallyWaitsToMinimizeStalling = false; } #if __IOS__ || __TVOS__ var avSession = AVAudioSession.SharedInstance(); // By setting the Audio Session category to AVAudioSessionCategorPlayback, audio will continue to play when the silent switch is enabled, or when the screen is locked. avSession.SetCategory(AVAudioSessionCategory.Playback); NSError activationError = null; avSession.SetActive(true, out activationError); if (activationError != null) { Console.WriteLine("Could not activate audio session {0}", activationError.LocalizedDescription); } #endif Player.AddObserver(this, (NSString)"rate", NSKeyValueObservingOptions.New | NSKeyValueObservingOptions.Initial, RateObservationContext.Handle); PeriodicTimeObserverObject = Player.AddPeriodicTimeObserver(new CMTime(1, 4), DispatchQueue.MainQueue, delegate { if (CurrentItem.Duration.IsInvalid || CurrentItem.Duration.IsIndefinite || double.IsNaN(CurrentItem.Duration.Seconds)) { PlayingChanged?.Invoke(this, new PlayingChangedEventArgs(0, Position, Duration)); } else { var totalDuration = TimeSpan.FromSeconds(CurrentItem.Duration.Seconds); var totalProgress = totalDuration.TotalMilliseconds == 0D ? 0D : Position.TotalMilliseconds / totalDuration.TotalMilliseconds; PlayingChanged?.Invoke(this, new PlayingChangedEventArgs( !double.IsInfinity(totalProgress) ? totalProgress : 0, Position, Duration )); } }); }
private void OnPlaying() { var progress = (Position.TotalSeconds / Duration.TotalSeconds); var position = Position; var duration = Duration; PlayingChanged?.Invoke(this, new PlayingChangedEventArgs( !double.IsInfinity(progress) ? progress : 0, position.TotalSeconds >= 0 ? position : TimeSpan.Zero, duration.TotalSeconds >= 0 ? duration : TimeSpan.Zero)); }
private void InitializePlayer() { _player = new AVPlayer(); Player.AddPeriodicTimeObserver(new CMTime(1, 4), DispatchQueue.MainQueue, delegate { var totalDuration = TimeSpan.FromSeconds(_player.CurrentItem.Duration.Seconds); var totalProgress = Position.TotalMilliseconds / totalDuration.TotalMilliseconds; PlayingChanged?.Invoke(this, new PlayingChangedEventArgs(totalProgress, Position, Duration)); }); }
public void Stop() { if (!Playing) { return; } _waveOut.Stop(); Playing = false; PlayingChanged?.Invoke(this, EventArgs.Empty); }
private void OnPlayingChanged(object sender, PlayingChangedEventArgs e) { if (sender == CurrentPlaybackManager) { if (!_startedPlaying && Duration != TimeSpan.Zero) { MediaNotificationManager?.UpdateNotifications(MediaQueue.Current, Status); _startedPlaying = true; } PlayingChanged?.Invoke(sender, e); } }
private void OnPlaying() { if (!IsReadyRendering) { CancelPlayingHandler(); //RenderSurface is no longer valid => Cancel the periodic firing } var progress = (Position.TotalSeconds / Duration.TotalSeconds); var position = Position; var duration = Duration; PlayingChanged?.Invoke(this, new PlayingChangedEventArgs( !double.IsInfinity(progress) ? progress : 0, position.TotalSeconds >= 0 ? position : TimeSpan.Zero, duration.TotalSeconds >= 0 ? duration : TimeSpan.Zero)); }
public void Play() { if (Playing) { return; } if (_source == null) { return; } _waveOut.Play(); Playing = true; PlayingChanged?.Invoke(this, EventArgs.Empty); }
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); }
private void InitializePlayer() { _player = new AVPlayer(); _videoLayer = AVPlayerLayer.FromPlayer(_player); NSError activationError = null; if (activationError != null) { Console.WriteLine("Could not activate audio session {0}", activationError.LocalizedDescription); } Player.AddPeriodicTimeObserver(new CMTime(1, 4), DispatchQueue.MainQueue, delegate { var totalDuration = TimeSpan.FromSeconds(_player.CurrentItem.Duration.Seconds); var totalProgress = Position.TotalMilliseconds / totalDuration.TotalMilliseconds; PlayingChanged?.Invoke(this, new PlayingChangedEventArgs(totalProgress, Position, Duration)); }); }
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); }
//private CollectionView myCollectionView; /// <summary> /// 从播放列表中播放音乐 /// </summary> /// <param name="NextPlay">选择当出现意外播放时的行为</param> static public void PlayAndExceptionPass(string NextPlay = "next") { if (PlayListIndex == 0) { return; } CurSongInfoEx = PlayListSongs[PlayListIndex - 1]; SongInfo sinfo = CurSongInfoEx.SongInfo; //播放相关 if (sinfo.SongPath == null || !File.Exists(sinfo.SongPath)) { ExceptionCount++; if (ExceptionCount > PlayListTotal) { MessageBox.Show("全曲目播放失败,暂停循环"); return; } if (NextPlay == "next" && ++PlayListIndex <= PlayListTotal) { PlayAndExceptionPass("next"); } else if (NextPlay == "prev" && --PlayListIndex >= 1) { PlayAndExceptionPass("prev"); } return; } //player.Open(sinfo.SongPath); //player.Play(); //try //{ // if (player.CurrentPlayerName == "naudio") // player.Open(sinfo.SongPath); // else // { // player.Stop(); // player = naudioplayer; // player.Open(sinfo.SongPath); // } //} //catch (NotSupportedException) //{ // player = cscplayer; // try // { // cscplayer.Open(sinfo.SongPath); // } // catch (Exception) // { // ++PlayListIndex; // PlayAndExceptionPass(); // } //} //player.CurPlayState = PlayState.playing; //SendMsg(MyMsgType.Startplay, sinfo.SongPath); ExceptionCount = 0; //LRC相关 Playing.lrcshow = null; Playing.CurLrcItem = null; //Playing.LoadLoaclLrc(null, null, null, null); PlayThread = new Thread(() => { try { player.Open(sinfo.SongPath, ConfigPage.GlobalConfig.CurrentVolume, ConfigPage.GlobalConfig.OpenMethodsStr, ConfigPage.GlobalConfig.DeviceStr); player.Play(); } catch { return; } PlayingChanged?.Invoke(CurSongInfoEx); timer.Change(1000, 1000);//这个延时大概是为了处理加载的结束;待改 }); PlayThread.Start(); // Thread.Sleep(10000); }
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; }