Пауза ExoPlayer, когда наушники отключены - PullRequest
0 голосов
/ 02 сентября 2018

Как изменить состояние воспроизведения ExoPlayer при запуске широковещательного приемника?

У меня есть BroadcastReceiver, который срабатывает, когда гарнитура отключена, однако я не уверен в правильном способе приостановки звука при вызове onReceive (). Нужно ли здесь менять состояние воспроизведения Mediasession?

private static final int CORRECT_ANSWER_DELAY_MILLIS = 1000;
public static final String TAG = QuizActivity.class.getSimpleName();
private SimpleExoPlayer mExoPlayer;
private PlayerView mPlayerView;
private PlayerListener mPlayerListener;
private PlaybackStateCompat.Builder mPlaybackStateBuilder;
public static MediaSessionCompat mMediaSession;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    initializeMediaSession();

    mPlayerListener = new PlayerListener();

    // Initialize the player view.
    mPlayerView = (PlayerView) findViewById(R.id.playerView);


    // Initialize the player.
    initializePlayer(Uri.parse(answerSample.getUri()));

}

/**
 * Initialize ExoPlayer.
 *
 * @param mediaUri The URI of the sample to play.
 */
// done (2): Set the ExoPlayer.EventListener to this activity
private void initializePlayer(Uri mediaUri) {

    // 1. Create a default TrackSelector

    if (mExoPlayer == null) {
        Handler mainHandler = new Handler();
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory =
                new AdaptiveTrackSelection.Factory(bandwidthMeter);
        DefaultTrackSelector trackSelector =
                new DefaultTrackSelector(videoTrackSelectionFactory);

        // 2. Create the player
        mExoPlayer =
                ExoPlayerFactory.newSimpleInstance(this, trackSelector);

        mExoPlayer.addListener(mPlayerListener);
        mPlayerView.setPlayer(mExoPlayer);


        DefaultBandwidthMeter defaultBandwidthMeter = new DefaultBandwidthMeter();
        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
                Util.getUserAgent(this, "yourApplicationName"), defaultBandwidthMeter);
        // This is the MediaSource representing the media to be played.
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory)
                .createMediaSource(mediaUri);
        // Prepare the player with the source.
        mExoPlayer.prepare(videoSource);
        mExoPlayer.setPlayWhenReady(true);
    }
}


/**
 * Release ExoPlayer.
 */
private void releasePlayer() {

    mExoPlayer.stop();
    mExoPlayer.release();
    mExoPlayer = null;
}


 * toggles the UI to show the correct answer.
 *
 * @param v The button that was clicked.
 */
@Override
public void onClick(View v) {


    // Wait some time so the user can see the correct answer, then go to the next question.
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            mExoPlayer.stop();
            Intent nextQuestionIntent = new Intent(QuizActivity.this, QuizActivity.class);
            nextQuestionIntent.putExtra(REMAINING_SONGS_KEY, mRemainingSampleIDs);
            finish();
            startActivity(nextQuestionIntent);
        }
    }, CORRECT_ANSWER_DELAY_MILLIS);
}



/**
 * Release the player when the activity is destroyed.
 */
@Override
protected void onDestroy() {
    super.onDestroy();
    releasePlayer();
    mMediaSession.setActive(false);
}


private void initializeMediaSession() {

    mMediaSession = new MediaSessionCompat(this, TAG);

    mMediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    // Do not let MediaButtons restart the player when the app is not visible.
       mMediaSession.setMediaButtonReceiver(null);


    mPlaybackStateBuilder = new PlaybackStateCompat.Builder()
            // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player.
            .setActions(PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PAUSE |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                    PlaybackStateCompat.ACTION_PLAY_PAUSE);

    mMediaSession.setPlaybackState(mPlaybackStateBuilder.build());

    mMediaSession.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public void onPlay() {

            mExoPlayer.setPlayWhenReady(true);
        }

        @Override
        public void onPause() {

            mExoPlayer.setPlayWhenReady(false);
        }

    });

    // Start the Media Session since the activity is active.
    mMediaSession.setActive(true);

}



public class PlayerListener extends Player.DefaultEventListener {
    @Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        //super.onPlayerStateChanged(playWhenReady, playbackState);
        if (playbackState == Player.STATE_READY && playWhenReady) {
            mPlaybackStateBuilder.setState(STATE_PLAYING, mExoPlayer.getCurrentPosition(), 1f);
        } else if (playbackState == Player.STATE_READY) {
            mPlaybackStateBuilder.setState(STATE_PAUSED, mExoPlayer.getCurrentPosition(), 1f);
        }
            mMediaSession.setPlaybackState(mPlaybackStateBuilder.build());
            showMediaStyleNotification(mPlaybackStateBuilder.build());
    }
}

public static class MediaReceiver extends BroadcastReceiver {

    public MediaReceiver() { }

    @Override
    public void onReceive(final Context context, Intent intent) {
        if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {


            Toast.makeText(context, "Headset unplugged", Toast.LENGTH_SHORT).show();


        }
   }
}

1 Ответ

0 голосов
/ 02 сентября 2018

Вы должны сначала зарегистрировать Получателя в Деятельности, как -

MediaReceiver mr = new MediaReceiver(mPlayerView);
registerReceiver(mr);

тогда

private class MediaReceiver extends BroadcastReceiver {
        private PlayerView pv;

        public MediaReceiver(PlayerView playerView) {
            this.pv = playerView;
        } 

        @Override 
        public void onReceive(final Context context, Intent intent) {
            if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) { 
                Toast.makeText(context, "Headset unplugged", Toast.LENGTH_SHORT).show(); 
                pv.pause();
            }
        }

также не забудьте отменить регистрацию Receiver в onDestroy () или onPause ()

...