本文整理汇总了Java中android.support.v4.media.session.PlaybackStateCompat.STATE_PLAYING属性的典型用法代码示例。如果您正苦于以下问题:Java PlaybackStateCompat.STATE_PLAYING属性的具体用法?Java PlaybackStateCompat.STATE_PLAYING怎么用?Java PlaybackStateCompat.STATE_PLAYING使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.support.v4.media.session.PlaybackStateCompat
的用法示例。
在下文中一共展示了PlaybackStateCompat.STATE_PLAYING属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: updateRemoteControlClient
/**
* Updates the lockscreen controls.
*
* @param what The broadcast
*/
private void updateRemoteControlClient(final String what) {
int playState = isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
PlaybackStateCompat pmc = new PlaybackStateCompat.Builder()
.setState(playState, position(), 1.0f)
.setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
.build();
switch (what) {
case PLAYSTATE_CHANGED:
case POSITION_CHANGED:
mMediaSession.setPlaybackState(pmc);
break;
case META_CHANGED:
mMediaSession.setMetadata(new MediaMetadataCompat.Builder()
.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
.putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
.putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName())
//.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, getAlbumCover())
.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration())
.build());
break;
}
}
示例2: run
@Override
public void run() {
int state = mediaPlayer.getPlaybackState();
if (state == ExoPlayer.STATE_READY) {
long pos = mediaPlayer.getCurrentPosition();
float speed = mediaPlayer.getPlaybackParameters().speed;
if (!notificationManager.startNotification()) {
notificationManager.updateNotification();
}
stateBuilder.setBufferedPosition(mediaPlayer.getBufferedPercentage());
int sessionState = mediaPlayer.getPlayWhenReady() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
mediaSession.setPlaybackState(stateBuilder.setState(sessionState, pos, speed).build());
if (mediaPlayer.getPlayWhenReady()) {
progressHandler.postDelayed(progressRunnable, 1000 - (pos % 1000));
}
}
}
示例3: updatePlayer
@Override
public void updatePlayer() {
if (player != null) {
switch (focusState) {
case AUDIO_NO_FOCUS_NO_DUCK:
if (playerState == PlaybackStateCompat.STATE_PLAYING) {
pause();
}
return;
case AUDIO_NO_FOCUS_CAN_DUCK:
registerNoiseReceiver();
player.setVolume(VOLUME_DUCK, VOLUME_DUCK);
break;
default:
registerNoiseReceiver();
player.setVolume(VOLUME_NORMAL,VOLUME_NORMAL);
}
player.start();
playerState=PlaybackStateCompat.STATE_PLAYING;
if(callback!=null) callback.onPlay();
}
}
示例4: getPlayPauseAction
private NotificationCompat.Action getPlayPauseAction() {
int icon;
String title;
PendingIntent pi;
if (mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
icon = android.R.drawable.ic_media_pause;
title = "pause";
pi = mPauseIntent;
} else {
icon = android.R.drawable.ic_media_play;
title = "play";
pi = mPlayIntent;
}
return new NotificationCompat.Action(icon, title, pi);
}
示例5: onSeekComplete
@Override
public void onSeekComplete(MediaPlayer mediaPlayer) {
Log.i(TAG, "onSeekComplete, current position: " + mCurrentPosition);
mCurrentPosition = mediaPlayer.getCurrentPosition();
if (mPlaybackState == PlaybackStateCompat.STATE_BUFFERING) {
mPlayer.start();
mPlaybackState = PlaybackStateCompat.STATE_PLAYING;
}
if (mCallback != null) {
mCallback.onPlayStateChanged(mPlaybackState);
}
}
示例6: configMediaPlayerState
/**
* Reconfigures MediaPlayer according to audio focus settings and
* starts/restarts it. This method starts/restarts the MediaPlayer
* respecting the current audio focus state. So if we have focus, it will
* play normally; if we don't have focus, it will either leave the
* MediaPlayer paused or set it to a low volume, depending on what is
* allowed by the current focus settings. This method assumes mPlayer !=
* null, so if you are calling it, you have to do so from a context where
* you are sure this is the case.
*/
private void configMediaPlayerState() {
if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
// If we don't have audio focus and can't duck, we have to pause,
if (mState == PlaybackStateCompat.STATE_PLAYING) {
pause();
}
} else { // we have audio focus:
registerAudioNoisyReceiver();
if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
} else {
if (mMediaPlayer != null) {
mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
} // else do something for remote client.
}
// If we were playing when we lost focus, we need to resume playing.
if (mPlayOnFocusGain) {
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
} else {
mMediaPlayer.seekTo(mCurrentPosition);
mState = PlaybackStateCompat.STATE_BUFFERING;
}
}
mPlayOnFocusGain = false;
}
}
if (mCallback != null) {
mCallback.onPlaybackStateChanged(mState);
}
}
示例7: configMediaPlayerState
private void configMediaPlayerState() {
Logger.d(TAG, "configMediaPlayerState. mAudioFocus=", mAudioFocus);
if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
// If we don't have audio focus and can't duck, we have to pause,
if (mState == PlaybackStateCompat.STATE_PLAYING) {
mPlayOnFocusGain = false;
pause();
}
} else { // we have audio focus:
registerAudioNoisyReceiver();
if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
} else {
if (mMediaPlayer != null) {
mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
} // else do something for remote client.
}
// If we were playing when we lost focus, we need to resume playing.
if (mPlayOnFocusGain) {
if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
Logger.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to ",
mCurrentPosition);
if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
} else {
mMediaPlayer.seekTo(mCurrentPosition);
mState = PlaybackStateCompat.STATE_BUFFERING;
}
}
mPlayOnFocusGain = false;
}
}
if (mCallback != null) {
mCallback.onPlaybackStatusChanged(mState);
}
}
示例8: setNewState
private void setNewState(@PlaybackStateCompat.State int newPlayerState) {
mState = newPlayerState;
// Whether playback goes to completion, or whether it is stopped, the
// mCurrentMediaPlayedToCompletion is set to true.
if (mState == PlaybackStateCompat.STATE_STOPPED) {
mCurrentMediaPlayedToCompletion = true;
}
// Work around for MediaPlayer.getCurrentPosition() when it changes while not playing.
final long reportPosition;
if (mSeekWhileNotPlaying >= 0) {
reportPosition = mSeekWhileNotPlaying;
if (mState == PlaybackStateCompat.STATE_PLAYING) {
mSeekWhileNotPlaying = -1;
}
} else {
reportPosition = mMediaPlayer == null ? 0 : mMediaPlayer.getCurrentPosition();
}
final PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
stateBuilder.setActions(getAvailableActions());
stateBuilder.setState(mState,
reportPosition,
1.0f,
SystemClock.elapsedRealtime());
mPlaybackInfoListener.onPlaybackStateChange(stateBuilder.build());
}
示例9: onAudioFocusChange
/**
* Called by AudioManager on audio focus changes.
* Implementation of {@link AudioManager.OnAudioFocusChangeListener}
*/
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// We have gained focus:
mAudioFocus = AUDIO_FOCUSED;
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// We have lost focus. If we can duck (low playback volume), we can keep playing.
// Otherwise, we need to pause the playback.
boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;
// If we are playing, we need to reset media player by calling configMediaPlayerState
// with mAudioFocus properly set.
if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
// If we don't have audio focus and can't duck, we save the information that
// we were playing, so that we can resume playback once we get the focus back.
mPlayOnFocusGain = true;
}
} else {
Log.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: "+ focusChange);
}
configMediaPlayerState();
}
示例10: onAudioFocusChange
/**
* Called by AudioManager on audio focus changes. Implementation of {@link
* AudioManager.OnAudioFocusChangeListener}
*/
@Override
public void onAudioFocusChange(int focusChange) {
boolean gotFullFocus = false;
boolean canDuck = false;
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
gotFullFocus = true;
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// We have lost focus. If we can duck (low playback volume), we can keep playing.
// Otherwise, we need to pause the playback.
canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
}
if (gotFullFocus || canDuck) {
if (mMediaPlayer != null) {
if (mPlayOnFocusGain) {
mPlayOnFocusGain = false;
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
updatePlaybackState();
}
float volume = canDuck ? 0.2f : 1.0f;
mMediaPlayer.setVolume(volume, volume);
}
} else if (mState == PlaybackStateCompat.STATE_PLAYING) {
mMediaPlayer.pause();
mState = PlaybackStateCompat.STATE_PAUSED;
updatePlaybackState();
}
}
示例11: onAudioFocusChange
/**
* Called by AudioManager on audio focus changes. Implementation of {@link
* AudioManager.OnAudioFocusChangeListener}.
*/
@Override
public void onAudioFocusChange(int focusChange) {
boolean gotFullFocus = false;
boolean canDuck = false;
if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
gotFullFocus = true;
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT
|| focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
// We have lost focus. If we can duck (low playback volume), we can keep playing.
// Otherwise, we need to pause the playback.
canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
}
if (gotFullFocus || canDuck) {
if (mMediaPlayer != null) {
if (mPlayOnFocusGain) {
mPlayOnFocusGain = false;
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
updatePlaybackState();
}
float volume = canDuck ? 0.2f : 1.0f;
mMediaPlayer.setVolume(volume, volume);
}
} else if (mState == PlaybackStateCompat.STATE_PLAYING) {
mMediaPlayer.pause();
mState = PlaybackStateCompat.STATE_PAUSED;
updatePlaybackState();
}
}
示例12: onSeekComplete
/**
* Called when MediaPlayer has completed a seek
*
* @see OnSeekCompleteListener
*/
@Override
public void onSeekComplete(MediaPlayer mp) {
mCurrentPosition = mp.getCurrentPosition();
if (mState == PlaybackStateCompat.STATE_BUFFERING) {
registerAudioNoisyReceiver();
mMediaPlayer.start();
mState = PlaybackStateCompat.STATE_PLAYING;
}
if (mCallback != null) {
mCallback.onPlaybackStateChanged(mState);
}
}
示例13: onPlay
@Override
public void onPlay() {
if (!exoPlayer.getPlayWhenReady()) {
startService(new Intent(getApplicationContext(), PlayerService.class));
MusicRepository.Track track = musicRepository.getCurrent();
updateMetadataFromTrack(track);
prepareToPlay(track.getUri());
if (!audioFocusRequested) {
audioFocusRequested = true;
int audioFocusResult;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioFocusResult = audioManager.requestAudioFocus(audioFocusRequest);
} else {
audioFocusResult = audioManager.requestAudioFocus(audioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
if (audioFocusResult != AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
return;
}
mediaSession.setActive(true); // Сразу после получения фокуса
registerReceiver(becomingNoisyReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
exoPlayer.setPlayWhenReady(true);
}
mediaSession.setPlaybackState(stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1).build());
currentState = PlaybackStateCompat.STATE_PLAYING;
refreshNotificationAndForegroundStatus(currentState);
}
示例14: configMediaPlayerState
private void configMediaPlayerState() {
Log.i(TAG, "configMediaPlayerState audio focus = " + mFocusState);
if (mFocusState == AUDIO_NO_FOCUS_NO_DUCK) {
if (mPlaybackState == PlaybackStateCompat.STATE_PLAYING) {
pause();
}
} else {
if (mFocusState == AUDIO_NO_FOCUS_CAN_DUCK) {
if (mPlayer != null) {
mPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK);
}
} else {
if (mPlayer != null) {
mPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL);
}
}
if (mPlayOnFocusGain) {
if (mPlayer != null && !mPlayer.isPlaying()) {
Log.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to "
+ getCurrentStreamPosition());
if (mCurrentPosition == mPlayer.getCurrentPosition()) {
mPlayer.start();
mPlaybackState = PlaybackStateCompat.STATE_PLAYING;
} else {
mPlayer.seekTo(mCurrentPosition);
mPlaybackState = PlaybackStateCompat.STATE_BUFFERING;
}
}
mPlayOnFocusGain = false;
}
}
if (mCallback != null) {
mCallback.onPlayStateChanged(mPlaybackState);
}
}
示例15: getNotification
public Notification getNotification(MediaMetadataCompat metadata,
@NonNull PlaybackStateCompat state,
MediaSessionCompat.Token token) {
boolean isPlaying = state.getState() == PlaybackStateCompat.STATE_PLAYING;
MediaDescriptionCompat description = metadata.getDescription();
NotificationCompat.Builder builder =
buildNotification(state, token, isPlaying, description);
return builder.build();
}