Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add exoplayer listener #22

Open
wants to merge 1 commit into
base: TMED.03-Exercise-EventListening
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,35 @@
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;
import com.google.android.exoplayer2.util.Util;

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};
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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() {

}

}