Skip to content
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 @@ -24,6 +24,8 @@
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
Expand Down Expand Up @@ -65,6 +67,9 @@ public class QuizActivity extends AppCompatActivity implements View.OnClickListe
private SimpleExoPlayer mExoPlayer;
private SimpleExoPlayerView mPlayerView;

private MediaSessionCompat mMediaSession;
private PlaybackStateCompat.Builder mStateBuilder;


@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -108,9 +113,10 @@ protected void onCreate(Bundle savedInstanceState) {
// Initialize the buttons with the composers names.
mButtons = initializeButtons(mQuestionSampleIDs);

// TODO (1): Create a method to initialize the MediaSession. It should create the MediaSessionCompat object, set the flags for external clients, set the available actions you want to support, and start the session.
// TODO (2): Create an inner class that extends MediaSessionCompat.Callbacks, and override the onPlay(), onPause(), and onSkipToPrevious() callbacks. Pass an instance of this class into the MediaSession.setCallback() method in the method you created in TODO 1.

// (1): Create a method to initialize the MediaSession. It should create the MediaSessionCompat object, set the flags for external clients, set the available actions you want to support, and start the session.
// (2): Create an inner class that extends MediaSessionCompat.Callbacks, and override the onPlay(), onPause(), and onSkipToPrevious() callbacks. Pass an instance of this class into the MediaSession.setCallback() method in the method you created in TODO 1.

initializeMediaSession();
Sample answerSample = Sample.getSampleByID(this, mAnswerSampleID);

if (answerSample == null) {
Expand All @@ -123,6 +129,31 @@ protected void onCreate(Bundle savedInstanceState) {
initializePlayer(Uri.parse(answerSample.getUri()));
}

private void initializeMediaSession() {
//create a mediaSessionCompat
mMediaSession = new MediaSessionCompat(this, TAG);
//Enable callbacks from MediaButtons and TransportControls
mMediaSession.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
//不让MediaButtons重启当app不可见的时候
mMediaSession.setMediaButtonReceiver(null);
mStateBuilder = new PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY |
PlaybackStateCompat.ACTION_PAUSE |
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
PlaybackStateCompat.ACTION_PLAY_PAUSE
);
mMediaSession.setPlaybackState(mStateBuilder.build());

mMediaSession.setCallback(new MySessionCallBack());
;
//打开Media Session
mMediaSession.setActive(true);

}


/**
* Initializes the button to the correct views, and sets the text to the composers names,
Expand All @@ -148,6 +179,7 @@ private Button[] initializeButtons(ArrayList<Integer> answerSampleIDs) {

/**
* Initialize ExoPlayer.
*
* @param mediaUri The URI of the sample to play.
*/
private void initializePlayer(Uri mediaUri) {
Expand All @@ -160,7 +192,7 @@ private void initializePlayer(Uri mediaUri) {

// Set the ExoPlayer.EventListener to this activity.
mExoPlayer.addListener(this);

// Prepare the MediaSource.
String userAgent = Util.getUserAgent(this, "ClassicalMusicQuiz");
MediaSource mediaSource = new ExtractorMediaSource(mediaUri, new DefaultDataSourceFactory(
Expand Down Expand Up @@ -266,12 +298,13 @@ private void showCorrectAnswer() {
*/
@Override
protected void onDestroy() {
// TODO (4): When the activity is destroyed, set the MediaSession to inactive.
// (4): When the activity is destroyed, set the MediaSession to inactive.
super.onDestroy();
releasePlayer();
mMediaSession.setActive(false);
}


// ExoPlayer Event Listeners

@Override
Expand All @@ -288,13 +321,18 @@ public void onLoadingChanged(boolean isLoading) {

@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
if((playbackState == ExoPlayer.STATE_READY) && playWhenReady){
// TODO (3): When ExoPlayer is playing, update the PlayBackState.
if ((playbackState == ExoPlayer.STATE_READY) && playWhenReady) {
// (3): When ExoPlayer is playing, update the PlayBackState.
Log.d(TAG, "onPlayerStateChanged: PLAYING");
} else if((playbackState == ExoPlayer.STATE_READY)){
// TODO (3): When ExoPlayer is paused, update the PlayBackState.
mStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING,
mExoPlayer.getCurrentPosition(), 1f);
} else if ((playbackState == ExoPlayer.STATE_READY)) {
// (3): When ExoPlayer is paused, update the PlayBackState.
Log.d(TAG, "onPlayerStateChanged: PAUSED");
mStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED,
mExoPlayer.getCurrentPosition(), 1f);
}
mMediaSession.setPlaybackState(mStateBuilder.build());
}

@Override
Expand All @@ -304,4 +342,22 @@ public void onPlayerError(ExoPlaybackException error) {
@Override
public void onPositionDiscontinuity() {
}


private class MySessionCallBack extends MediaSessionCompat.Callback {
@Override
public void onPlay() {
mExoPlayer.setPlayWhenReady(true);
}

@Override
public void onPause() {
mExoPlayer.setPlayWhenReady(false);
}

@Override
public void onSkipToPrevious() {
mExoPlayer.seekTo(0);
}
}
}