use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.
the class PlaybackManagerTest method testPlay.
@Test
public void testPlay() throws Exception {
String mediaId = MediaIDHelper.MEDIA_ID_ROOT;
while (MediaIDHelper.isBrowseable(mediaId)) {
mediaId = musicProvider.getChildren(mediaId, resources).get(0).getMediaId();
}
// Using a CountDownLatch, we will check if all callbacks are called correctly when
// a onPlayFromMediaId command is issued.
final CountDownLatch latch = new CountDownLatch(5);
final String expectedMediaId = mediaId;
QueueManager queueManager = new QueueManager(musicProvider, resources, new SimpleMetadataUpdateListener() {
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
// Latch countdown 1: QueueManager will change appropriately
assertEquals(MediaIDHelper.extractMusicIDFromMediaID(expectedMediaId), metadata.getDescription().getMediaId());
latch.countDown();
}
});
SimplePlaybackServiceCallback serviceCallback = new SimplePlaybackServiceCallback() {
@Override
public void onPlaybackStart() {
// Latch countdown 2: PlaybackService will get a onPlaybackStart call
latch.countDown();
}
@Override
public void onPlaybackStateUpdated(PlaybackStateCompat newState) {
if (newState.getState() == PlaybackStateCompat.STATE_PLAYING) {
// Latch countdown 3: PlaybackService will get a state updated call (here we
// ignore the unrelated state changes)
latch.countDown();
}
}
@Override
public void onNotificationRequired() {
// Latch countdown 4: PlaybackService will get call to show a media notification
latch.countDown();
}
};
Playback playback = new SimplePlayback() {
@Override
public void play(MediaSessionCompat.QueueItem item) {
// Latch countdown 5: Playback will be called with the correct queueItem
assertEquals(expectedMediaId, item.getDescription().getMediaId());
latch.countDown();
}
};
PlaybackManager playbackManager = new PlaybackManager(serviceCallback, resources, musicProvider, queueManager, playback);
playbackManager.getMediaSessionCallback().onPlayFromMediaId(expectedMediaId, null);
latch.await(5, TimeUnit.SECONDS);
// Finally, check if the current music in queueManager is as expected
assertEquals(expectedMediaId, queueManager.getCurrentMusic().getDescription().getMediaId());
}
use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.
the class FullScreenPlayerActivity method onCreate.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_player);
initializeToolbar();
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("");
}
mBackgroundImage = (ImageView) findViewById(R.id.background_image);
mPauseDrawable = ContextCompat.getDrawable(this, R.drawable.uamp_ic_pause_white_48dp);
mPlayDrawable = ContextCompat.getDrawable(this, R.drawable.uamp_ic_play_arrow_white_48dp);
mPlayPause = (ImageView) findViewById(R.id.play_pause);
mSkipNext = (ImageView) findViewById(R.id.next);
mSkipPrev = (ImageView) findViewById(R.id.prev);
mStart = (TextView) findViewById(R.id.startText);
mEnd = (TextView) findViewById(R.id.endText);
mSeekbar = (SeekBar) findViewById(R.id.seekBar1);
mLine1 = (TextView) findViewById(R.id.line1);
mLine2 = (TextView) findViewById(R.id.line2);
mLine3 = (TextView) findViewById(R.id.line3);
mLoading = (ProgressBar) findViewById(R.id.progressBar1);
mControllers = findViewById(R.id.controllers);
mSkipNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaControllerCompat.TransportControls controls = MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this).getTransportControls();
controls.skipToNext();
}
});
mSkipPrev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MediaControllerCompat.TransportControls controls = MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this).getTransportControls();
controls.skipToPrevious();
}
});
mPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PlaybackStateCompat state = MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this).getPlaybackState();
if (state != null) {
MediaControllerCompat.TransportControls controls = MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this).getTransportControls();
switch(state.getState()) {
// fall through
case PlaybackStateCompat.STATE_PLAYING:
case PlaybackStateCompat.STATE_BUFFERING:
controls.pause();
stopSeekbarUpdate();
break;
case PlaybackStateCompat.STATE_PAUSED:
case PlaybackStateCompat.STATE_STOPPED:
controls.play();
scheduleSeekbarUpdate();
break;
default:
LogHelper.d(TAG, "onClick with state ", state.getState());
}
}
}
});
mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mStart.setText(DateUtils.formatElapsedTime(progress / 1000));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
stopSeekbarUpdate();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this).getTransportControls().seekTo(seekBar.getProgress());
scheduleSeekbarUpdate();
}
});
// Only update from the intent if we are not recreating from a config change:
if (savedInstanceState == null) {
updateFromParams(getIntent());
}
mMediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MusicService.class), mConnectionCallback, null);
}
use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.
the class MediaItemViewHolder method getStateFromController.
public static int getStateFromController(Activity context) {
MediaControllerCompat controller = MediaControllerCompat.getMediaController(context);
PlaybackStateCompat pbState = controller.getPlaybackState();
if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
return MediaItemViewHolder.STATE_NONE;
} else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
return MediaItemViewHolder.STATE_PLAYING;
} else {
return MediaItemViewHolder.STATE_PAUSED;
}
}
use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.
the class PlaybackControlsFragment method onPlaybackStateChanged.
private void onPlaybackStateChanged(PlaybackStateCompat state) {
LogHelper.d(TAG, "onPlaybackStateChanged ", state);
if (getActivity() == null) {
LogHelper.w(TAG, "onPlaybackStateChanged called when getActivity null," + "this should not happen if the callback was properly unregistered. Ignoring.");
return;
}
if (state == null) {
return;
}
boolean enablePlay = false;
switch(state.getState()) {
case PlaybackStateCompat.STATE_PAUSED:
case PlaybackStateCompat.STATE_STOPPED:
enablePlay = true;
break;
case PlaybackStateCompat.STATE_ERROR:
LogHelper.e(TAG, "error playbackstate: ", state.getErrorMessage());
Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show();
break;
}
if (enablePlay) {
mPlayPause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_black_36dp));
} else {
mPlayPause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_black_36dp));
}
MediaControllerCompat controller = MediaControllerCompat.getMediaController(getActivity());
String extraInfo = null;
if (controller != null && controller.getExtras() != null) {
String castName = controller.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
if (castName != null) {
extraInfo = getResources().getString(R.string.casting_to_device, castName);
}
}
setExtraInfo(extraInfo);
}
use of android.support.v4.media.session.PlaybackStateCompat in project android-UniversalMusicPlayer by googlesamples.
the class TvPlaybackFragment method updatePlaybackState.
protected void updatePlaybackState(PlaybackStateCompat state) {
if (mPlaybackControlsRow == null) {
// We only update playback state after we get a valid metadata.
return;
}
mLastPosition = state.getPosition();
mLastPositionUpdateTime = state.getLastPositionUpdateTime();
switch(state.getState()) {
case PlaybackStateCompat.STATE_PLAYING:
startProgressAutomation();
mPlayPauseAction.setIndex(PlayPauseAction.PAUSE);
break;
case PlaybackStateCompat.STATE_PAUSED:
stopProgressAutomation();
mPlayPauseAction.setIndex(PlayPauseAction.PLAY);
break;
}
MediaControllerCompat controller = MediaControllerCompat.getMediaController(getActivity());
updatePlayListRow(controller.getQueue());
mRowsAdapter.notifyArrayItemRangeChanged(mRowsAdapter.indexOf(mPlaybackControlsRow), 1);
}
Aggregations