From c7480406dab7fccb671d73f587767e67a728cc57 Mon Sep 17 00:00:00 2001 From: Sun Date: Wed, 22 Aug 2018 19:52:33 +0800 Subject: [PATCH] add exoplayer listener --- .../classicalmusicquiz/QuizActivity.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java b/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java index 0539cd08..be51bc2f 100644 --- a/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java +++ b/app/src/main/java/com/example/android/classicalmusicquiz/QuizActivity.java @@ -25,18 +25,24 @@ import android.os.Handler; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.google.android.exoplayer2.DefaultLoadControl; +import com.google.android.exoplayer2.ExoPlaybackException; +import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayerFactory; import com.google.android.exoplayer2.LoadControl; import com.google.android.exoplayer2.SimpleExoPlayer; +import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory; import com.google.android.exoplayer2.source.ExtractorMediaSource; import com.google.android.exoplayer2.source.MediaSource; +import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; +import com.google.android.exoplayer2.trackselection.TrackSelectionArray; import com.google.android.exoplayer2.trackselection.TrackSelector; import com.google.android.exoplayer2.ui.SimpleExoPlayerView; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; @@ -44,9 +50,10 @@ import java.util.ArrayList; -// TODO (1): Have this Activity implement ExoPlayer.EventListener and add the required methods. -public class QuizActivity extends AppCompatActivity implements View.OnClickListener { +// (1): Have this Activity implement ExoPlayer.EventListener and add the required methods. +public class QuizActivity extends AppCompatActivity implements View.OnClickListener , ExoPlayer.EventListener{ + private static final String TAG = "QuizActivity"; private static final int CORRECT_ANSWER_DELAY_MILLIS = 1000; private static final String REMAINING_SONGS_KEY = "remaining_songs"; private int[] mButtonIDs = {R.id.buttonA, R.id.buttonB, R.id.buttonC, R.id.buttonD}; @@ -149,7 +156,9 @@ private void initializePlayer(Uri mediaUri) { mExoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl); mPlayerView.setPlayer(mExoPlayer); - // TODO (2): Set the ExoPlayer.EventListener to this activity + // (2): Set the ExoPlayer.EventListener to this activity + mExoPlayer.addListener(this); + // Prepare the MediaSource. String userAgent = Util.getUserAgent(this, "ClassicalMusicQuiz"); @@ -260,5 +269,43 @@ protected void onDestroy() { releasePlayer(); } - // TODO (3): Add conditional logging statements to the onPlayerStateChanged() method that log when ExoPlayer is playing or paused. + @Override + public void onTimelineChanged(Timeline timeline, Object manifest) { + + } + + @Override + public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { + + } + + @Override + public void onLoadingChanged(boolean isLoading) { + + } + + // (3): Add conditional logging statements to the onPlayerStateChanged() method that log when ExoPlayer is playing or paused. + @Override + public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { + //每次播放器状态发生变化时都会被调用,因此我们将在该方法中更新媒体会话。 + if((playbackState == ExoPlayer.STATE_READY) && playWhenReady ){ + //we are playing + Log.e(TAG, "onPlayerStateChanged: playing" ); + }else if(playbackState == ExoPlayer.STATE_READY){ + //we are pause + Log.e(TAG, "onPlayerStateChanged: pause" ); + } + + } + + @Override + public void onPlayerError(ExoPlaybackException error) { + + } + + @Override + public void onPositionDiscontinuity() { + + } + }