下面列出了android.media.session.PlaybackState#STATE_STOPPED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onClick(View v) {
final int state = mCurrentState == null ?
PlaybackState.STATE_NONE : mCurrentState.getState();
if (state == PlaybackState.STATE_PAUSED ||
state == PlaybackState.STATE_STOPPED ||
state == PlaybackState.STATE_NONE) {
if (mCurrentMetadata == null) {
mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
MusicLibrary.getMediaItems().get(0).getMediaId());
updateMetadata(mCurrentMetadata);
}
getMediaController().getTransportControls().playFromMediaId(
mCurrentMetadata.getDescription().getMediaId(), null);
} else {
getMediaController().getTransportControls().pause();
}
}
/**
* Check if the MediaSession is active and in a "playback-able" state
* (not NONE and not STOPPED).
*
* @return true if the MediaSession's state requires playback controls to be visible.
*/
protected boolean shouldShowControls() {
MediaControllerCompat mediaController = mMediaController;
if (mediaController == null ||
mediaController.getMetadata() == null ||
mediaController.getPlaybackState() == null) {
return false;
}
switch (mediaController.getPlaybackState().getState()) {
case PlaybackState.STATE_ERROR:
case PlaybackState.STATE_NONE:
case PlaybackState.STATE_STOPPED:
return false;
default:
return true;
}
}
@Override
public void onClick(View v) {
final int state = mCurrentState == null ?
PlaybackState.STATE_NONE : mCurrentState.getState();
if (state == PlaybackState.STATE_PAUSED ||
state == PlaybackState.STATE_STOPPED ||
state == PlaybackState.STATE_NONE) {
if (mCurrentMetadata == null) {
mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
MusicLibrary.getMediaItems().get(0).getMediaId());
updateMetadata(mCurrentMetadata);
}
getMediaController().getTransportControls().playFromMediaId(
mCurrentMetadata.getDescription().getMediaId(), null);
} else {
getMediaController().getTransportControls().pause();
}
}
private void updatePlaybackState(String error) {
long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;
MessageObject playingMessageObject = MediaController.getInstance().getPlayingMessageObject();
if (playingMessageObject != null) {
position = playingMessageObject.audioProgressSec * 1000;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder().setActions(getAvailableActions());
int state;
if (playingMessageObject == null) {
state = PlaybackState.STATE_STOPPED;
} else {
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
state = PlaybackState.STATE_BUFFERING;
} else {
state = MediaController.getInstance().isMessagePaused() ? PlaybackState.STATE_PAUSED : PlaybackState.STATE_PLAYING;
}
}
if (error != null) {
stateBuilder.setErrorMessage(error);
state = PlaybackState.STATE_ERROR;
}
stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());
if (playingMessageObject != null) {
stateBuilder.setActiveQueueItemId(MediaController.getInstance().getPlayingMessageObjectNum());
} else {
stateBuilder.setActiveQueueItemId(0);
}
mediaSession.setPlaybackState(stateBuilder.build());
}
private void updatePlaybackState(String error) {
long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;
MessageObject playingMessageObject = MediaController.getInstance().getPlayingMessageObject();
if (playingMessageObject != null) {
position = playingMessageObject.audioProgressSec * 1000;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder().setActions(getAvailableActions());
int state;
if (playingMessageObject == null) {
state = PlaybackState.STATE_STOPPED;
} else {
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
state = PlaybackState.STATE_BUFFERING;
} else {
state = MediaController.getInstance().isMessagePaused() ? PlaybackState.STATE_PAUSED : PlaybackState.STATE_PLAYING;
}
}
if (error != null) {
stateBuilder.setErrorMessage(error);
state = PlaybackState.STATE_ERROR;
}
stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());
if (playingMessageObject != null) {
stateBuilder.setActiveQueueItemId(MediaController.getInstance().getPlayingMessageObjectNum());
} else {
stateBuilder.setActiveQueueItemId(0);
}
mediaSession.setPlaybackState(stateBuilder.build());
}
private void updatePlaybackState(PlaybackState state) {
mCurrentState = state;
if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
state.getState() == PlaybackState.STATE_STOPPED) {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
} else {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
}
mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
@Override
public void onClick(View v) {
final int state = mCurrentState == null ?
PlaybackState.STATE_NONE : mCurrentState.getState();
if (state == PlaybackState.STATE_PAUSED ||
state == PlaybackState.STATE_STOPPED ||
state == PlaybackState.STATE_NONE) {
if (mCurrentMetadata == null) {
mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
MusicLibrary.getMediaItems().get(0).getMediaId());
updateMetadata(mCurrentMetadata);
}
// ------------ CHANGE 5 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
mPlaybackManager.play(mCurrentMetadata);
/* ------------ CHANGE 5 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
getMediaController().getTransportControls().playFromMediaId(
mCurrentMetadata.getDescription().getMediaId(), null);
// ------------ CHANGE 5 - END OF PLAYBACK ON A SERVICE SNIPPET */
} else {
// ------------ CHANGE 6 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
mPlaybackManager.pause();
/* ------------ CHANGE 6 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
getMediaController().getTransportControls().pause();
// ------------ CHANGE 6 - END OF PLAYBACK ON A SERVICE SNIPPET */
}
}
public void stop() {
mState = PlaybackState.STATE_STOPPED;
updatePlaybackState();
// Give up Audio focus
mAudioManager.abandonAudioFocus(this);
// Relax all resources
releaseMediaPlayer();
}
private void updatePlaybackState(PlaybackState state) {
mCurrentState = state;
if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
state.getState() == PlaybackState.STATE_STOPPED) {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
} else {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
}
mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
public void stop() {
mState = PlaybackState.STATE_STOPPED;
updatePlaybackState();
// Give up Audio focus
mAudioManager.abandonAudioFocus(this);
// Relax all resources
releaseMediaPlayer();
}
private void updatePlaybackState(PlaybackState state) {
mCurrentState = state;
if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
state.getState() == PlaybackState.STATE_STOPPED) {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
} else {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
}
mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
@Override
public void onClick(View v) {
final int state = mCurrentState == null ?
PlaybackState.STATE_NONE : mCurrentState.getState();
if (state == PlaybackState.STATE_PAUSED ||
state == PlaybackState.STATE_STOPPED ||
state == PlaybackState.STATE_NONE) {
if (mCurrentMetadata == null) {
mCurrentMetadata = MusicLibrary.getMetadata(MusicPlayerActivity.this,
MusicLibrary.getMediaItems().get(0).getMediaId());
updateMetadata(mCurrentMetadata);
}
// ------------ CHANGE 5 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
mPlaybackManager.play(mCurrentMetadata);
/* ------------ CHANGE 5 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
getMediaController().getTransportControls().playFromMediaId(
mCurrentMetadata.getDescription().getMediaId(), null);
// ------------ CHANGE 5 - END OF PLAYBACK ON A SERVICE SNIPPET */
} else {
// ------------ CHANGE 6 - REMOVE FOLLOWING LINES FOR PLAYBACK ON A SERVICE
mPlaybackManager.pause();
/* ------------ CHANGE 6 - UNCOMMENT FOLLOWING LINES FOR PLAYBACK ON A SERVICE
getMediaController().getTransportControls().pause();
// ------------ CHANGE 6 - END OF PLAYBACK ON A SERVICE SNIPPET */
}
}
public void stop() {
mState = PlaybackState.STATE_STOPPED;
updatePlaybackState();
// Give up Audio focus
mAudioManager.abandonAudioFocus(this);
// Relax all resources
releaseMediaPlayer();
}
private void updatePlaybackState(PlaybackState state) {
mCurrentState = state;
if (state == null || state.getState() == PlaybackState.STATE_PAUSED ||
state.getState() == PlaybackState.STATE_STOPPED) {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_play_arrow_black_36dp));
} else {
mPlayPause.setImageDrawable(getDrawable(R.drawable.ic_pause_black_36dp));
}
mPlaybackControls.setVisibility(state == null ? View.GONE : View.VISIBLE);
}
public void stop() {
mState = PlaybackState.STATE_STOPPED;
updatePlaybackState();
// Give up Audio focus
mAudioManager.abandonAudioFocus(this);
// Relax all resources
releaseMediaPlayer();
}
@Nullable
private MediaController pickBestMediaController(
@NonNull List<MediaController> list) {
int mediaControllerScore = -1;
MediaController mediaController = null;
for (MediaController mc : list) {
if (mc == null) continue;
int mcScore = 0;
// Check for the current state
PlaybackState state = mc.getPlaybackState();
if (state != null) {
switch (state.getState()) {
case PlaybackState.STATE_STOPPED:
case PlaybackState.STATE_ERROR:
break;
default:
mcScore++;
break;
}
}
if (mcScore > mediaControllerScore) {
mediaControllerScore = mcScore;
mediaController = mc;
}
}
return mediaController;
}
private void updatePlaybackState(String error) {
long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;
MessageObject playingMessageObject = MediaController.getInstance().getPlayingMessageObject();
if (playingMessageObject != null) {
position = playingMessageObject.audioProgressSec * 1000L;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder().setActions(getAvailableActions());
int state;
if (playingMessageObject == null) {
state = PlaybackState.STATE_STOPPED;
} else {
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
state = PlaybackState.STATE_BUFFERING;
} else {
state = MediaController.getInstance().isMessagePaused() ? PlaybackState.STATE_PAUSED : PlaybackState.STATE_PLAYING;
}
}
if (error != null) {
stateBuilder.setErrorMessage(error);
state = PlaybackState.STATE_ERROR;
}
stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());
if (playingMessageObject != null) {
stateBuilder.setActiveQueueItemId(MediaController.getInstance().getPlayingMessageObjectNum());
} else {
stateBuilder.setActiveQueueItemId(0);
}
mediaSession.setPlaybackState(stateBuilder.build());
}
private void updatePlaybackState(String error) {
long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;
MessageObject playingMessageObject = MediaController.getInstance().getPlayingMessageObject();
if (playingMessageObject != null) {
position = playingMessageObject.audioProgressSec * 1000L;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder().setActions(getAvailableActions());
int state;
if (playingMessageObject == null) {
state = PlaybackState.STATE_STOPPED;
} else {
if (MediaController.getInstance().isDownloadingCurrentMessage()) {
state = PlaybackState.STATE_BUFFERING;
} else {
state = MediaController.getInstance().isMessagePaused() ? PlaybackState.STATE_PAUSED : PlaybackState.STATE_PLAYING;
}
}
if (error != null) {
stateBuilder.setErrorMessage(error);
state = PlaybackState.STATE_ERROR;
}
stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());
if (playingMessageObject != null) {
stateBuilder.setActiveQueueItemId(MediaController.getInstance().getPlayingMessageObjectNum());
} else {
stateBuilder.setActiveQueueItemId(0);
}
mediaSession.setPlaybackState(stateBuilder.build());
}
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
state.getState() == PlaybackState.STATE_NONE) {
mService.stopForeground(true);
try {
mService.unregisterReceiver(this);
} catch (IllegalArgumentException ex) {
// ignore receiver not registered
}
mService.stopSelf();
return;
}
if (metadata == null) {
return;
}
boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
Notification.Builder notificationBuilder = new Notification.Builder(mService);
MediaDescription description = metadata.getDescription();
notificationBuilder
.setStyle(new Notification.MediaStyle()
.setMediaSession(token)
.setShowActionsInCompactView(0, 1, 2))
.setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(createContentIntent())
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
.setOngoing(isPlaying)
.setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
.setShowWhen(isPlaying)
.setUsesChronometer(isPlaying);
// If skip to next action is enabled
if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
notificationBuilder.addAction(mPrevAction);
}
notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);
// If skip to prev action is enabled
if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
notificationBuilder.addAction(mNextAction);
}
Notification notification = notificationBuilder.build();
if (isPlaying && !mStarted) {
mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
mService.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
} else {
if (!isPlaying) {
mService.stopForeground(false);
mStarted = false;
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}
public void update(MediaMetadata metadata, PlaybackState state, MediaSession.Token token) {
if (state == null || state.getState() == PlaybackState.STATE_STOPPED ||
state.getState() == PlaybackState.STATE_NONE) {
mService.stopForeground(true);
try {
mService.unregisterReceiver(this);
} catch (IllegalArgumentException ex) {
// ignore receiver not registered
}
mService.stopSelf();
return;
}
if (metadata == null) {
return;
}
boolean isPlaying = state.getState() == PlaybackState.STATE_PLAYING;
Notification.Builder notificationBuilder = new Notification.Builder(mService);
MediaDescription description = metadata.getDescription();
notificationBuilder
.setStyle(new Notification.MediaStyle()
.setMediaSession(token)
.setShowActionsInCompactView(0, 1, 2))
.setColor(mService.getApplication().getResources().getColor(R.color.notification_bg))
.setSmallIcon(R.drawable.ic_notification)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(createContentIntent())
.setContentTitle(description.getTitle())
.setContentText(description.getSubtitle())
.setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
.setOngoing(isPlaying)
.setWhen(isPlaying ? System.currentTimeMillis() - state.getPosition() : 0)
.setShowWhen(isPlaying)
.setUsesChronometer(isPlaying);
// If skip to next action is enabled
if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) != 0) {
notificationBuilder.addAction(mPrevAction);
}
notificationBuilder.addAction(isPlaying ? mPauseAction : mPlayAction);
// If skip to prev action is enabled
if ((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) != 0) {
notificationBuilder.addAction(mNextAction);
}
Notification notification = notificationBuilder.build();
if (isPlaying && !mStarted) {
mService.startService(new Intent(mService.getApplicationContext(), MusicService.class));
mService.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
} else {
if (!isPlaying) {
mService.stopForeground(false);
mStarted = false;
}
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
}