상속: BroadcastReceiver
예제 #1
0
        public virtual async Task Play(IMediaFile mediaFile = null)
        {
            if (!ValidateMediaFile(mediaFile))
            {
                return;
            }

            bool alreadyPlaying = await CheckIfFileAlreadyIsPlaying(mediaFile);

            if (alreadyPlaying)
            {
                return;
            }

            CurrentFile = mediaFile;

            bool dataSourceSet;

            try
            {
                InitializePlayer();
                SessionManager.InitMediaSession(PackageName, Binder as MediaServiceBinder);
                _noisyAudioStreamReceiver = new AudioPlayerBroadcastReceiver(SessionManager);
                _intentFilter             = new IntentFilter(AudioManager.ActionAudioBecomingNoisy);
                SessionManager.ApplicationContext.RegisterReceiver(_noisyAudioStreamReceiver, _intentFilter);
                dataSourceSet = await SetMediaPlayerDataSource();
            }
            catch (Exception ex)
            {
                dataSourceSet = false;
                OnMediaFileFailed(new MediaFileFailedEventArgs(ex, mediaFile));
            }

            if (dataSourceSet)
            {
                try
                {
                    var focusResult = AudioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
                    if (focusResult != AudioFocusRequest.Granted)
                    {
                        Console.WriteLine("Could not get audio focus");
                    }

                    AquireWifiLock();
                }
                catch (Exception ex)
                {
                    OnMediaFileFailed(new MediaFileFailedEventArgs(ex, CurrentFile));
                }
            }
        }
        public virtual async Task Play(IMediaFile mediaFile = null)
        {
            if (!ValidateMediaFile(mediaFile))
            {
                return;
            }

            bool alreadyPlaying = await CheckIfFileAlreadyIsPlaying(mediaFile);

            if (alreadyPlaying)
            {
                return;
            }

            CurrentFile = mediaFile;

            bool dataSourceSet;

            try
            {
                InitializePlayer();

                // Buffering states similar to how iOS has been implemented
                SessionManager.UpdatePlaybackState(PlaybackStateCompat.StateBuffering);

                SessionManager.InitMediaSession(PackageName, Binder as MediaServiceBinder);
                _noisyAudioStreamReceiver = new AudioPlayerBroadcastReceiver(SessionManager);
                _intentFilter             = new IntentFilter(AudioManager.ActionAudioBecomingNoisy);
                SessionManager.ApplicationContext.RegisterReceiver(_noisyAudioStreamReceiver, _intentFilter);
                dataSourceSet = await SetMediaPlayerDataSource();

                ((IAndroidMediaNotificationManager)SessionManager.NotificationManager).UpdateMetadata(mediaFile);
            }
            catch (Exception ex)
            {
                dataSourceSet = false;
                OnMediaFailed(new MediaFailedEventArgs(ex.ToString(), ex));
            }

            if (dataSourceSet)
            {
                try
                {
                    var audioAttributes = new AudioAttributes.Builder()
                                          .SetUsage(AudioUsageKind.Media)
                                          .SetContentType(AudioContentType.Music)
//                                                             .SetLegacyStreamType(Stream.Music)
                                          .Build();

                    //var audioFocusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
                    //                                                  .SetAudioAttributes(audioAttributes)
                    //                                                  .SetAcceptsDelayedFocusGain(true)
                    //                                                  .SetOnAudioFocusChangeListener(this)
                    //                                                  .Build();

                    //var focusResult = AudioManager.RequestAudioFocus(audioFocusRequest);
                    //if (focusResult != AudioFocusRequest.Granted)
                    //    Console.WriteLine("Could not get audio focus");

                    AcquireWifiLock();
                }
                catch (Exception ex)
                {
                    OnMediaFailed(new MediaFailedEventArgs(ex.ToString(), ex));
                }
            }
        }