Exemple #1
0
        public NativeAudioPlayer()
        {
            Shared = this;
            timer  = NSTimer.CreateRepeatingScheduledTimer(10, CheckPlaybackStatus);
            NSError error;

#if __IOS__
            AVAudioSession.SharedInstance().SetCategory(AVAudioSession.CategoryPlayback, out error);
#endif

            player = new AudioFadePlayer
            {
                Parent   = this,
                Finished = (obj) =>
                {
                    finishedPlaying(player.CurrentSong);
                },
            };

#if __IOS__
            silentPlayer = new AVAudioPlayer(NSBundle.MainBundle.GetUrlForResource("empty", "mp3"), "mp3", out error)
            {
                NumberOfLoops = -1,
            };

            PictureInPictureManager.Shared.Setup(VideoLayer);
            //			observer = player.AddObserver("rate", NSKeyValueObservingOptions.New, (change) =>
            //			{
            //				Console.WriteLine("Playback state changed: {0}", player.Rate);
            //				if (player.Rate == 0)
            //				{
            //					State = PlaybackState.Paused;
            //					Console.WriteLine("AVPlayer Paused");
            //				}
            //				else
            //				{
            //					State = PlaybackState.Playing;
            //					Console.WriteLine("AVPlayer Playing");
            //				}
            //			});
            //			NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, (notification) =>
            //			{
            //				finishedPlaying(currentSong);
            //				player.ReplaceCurrentItemWithPlayerItem(new AVPlayerItem(new NSUrl("")));
            //			});
            AVAudioSession.Notifications.ObserveInterruption((sender, args) =>
            {
                if (args.InterruptionType == AVAudioSessionInterruptionType.Began)
                {
                    lastState = State;
                    if (State == PlaybackState.Playing)
                    {
                        Pause();
                    }
                    else
                    {
                        State = PlaybackState.Stopped;
                    }
                    lastInterupt = DateTime.Now;
                }
                else if (args.InterruptionType == AVAudioSessionInterruptionType.Ended)
                {
                    State = lastState;
                    if (args.Option == AVAudioSessionInterruptionOptions.ShouldResume && lastState == PlaybackState.Playing)
                    {
                        Play();
                    }
                    else
                    {
                        Pause();
                    }
                }
                NotificationManager.Shared.ProcPlaybackStateChanged(State);
                Console.WriteLine("Interupted,: {2} -  {0} , {1}", args.InterruptionType, args.Option, (DateTime.Now - lastInterupt).TotalSeconds);
            });

            AVAudioSession.Notifications.ObserveRouteChange((sender, args) =>
            {
                if (args.Reason == AVAudioSessionRouteChangeReason.OldDeviceUnavailable)
                {
                    Pause();
                }
                Console.WriteLine("Route Changed");
            });
            NotificationManager.Shared.EqualizerEnabledChanged += (sender, e) =>
            {
                if (!Settings.EqualizerEnabled)
                {
                    for (int i = 0; i < Equalizer.Shared.Bands.Length; i++)
                    {
                        player?.UpdateBand(i, 0);
                    }
                }
                else
                {
                    ApplyEqualizer(Equalizer.Shared.Bands);
                }
            };
#endif
        }