下面列出了android.media.session.PlaybackState#Builder ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void updatePlaybackState(int playbackState) {
PlaybackState.Builder state = new PlaybackState.Builder();
long position = player.getCurrentPosition();
if (ExoPlayer.STATE_PREPARING == playbackState) {
state.setState(PlaybackState.STATE_CONNECTING, position, 1.0f);
} else if (ExoPlayer.STATE_BUFFERING == playbackState) {
state.setState(PlaybackState.STATE_BUFFERING, position, 1.0f);
} else {
if (player.getPlayerControl().isPlaying()) {
state.setState(PlaybackState.STATE_PLAYING, position, 1.0f);
} else {
state.setState(PlaybackState.STATE_PAUSED, position, 1.0f);
}
}
mediaSession.setPlaybackState(state.build());
}
private void updatePlayState() {
PlaybackState.Builder builder = new PlaybackState.Builder();
switch (App.Clementine.getState()) {
case PLAY:
builder.setState(PlaybackState.STATE_PLAYING, App.Clementine.getSongPosition(),
1.0f);
break;
case PAUSE:
builder.setState(PlaybackState.STATE_PAUSED, App.Clementine.getSongPosition(),
1.0f);
break;
case STOP:
builder.setState(PlaybackState.STATE_STOPPED, 0, 1.0f);
break;
default:
break;
}
mMediaSession.setPlaybackState(builder.build());
}
private PlaybackState getStateWithUpdatedPosition() {
PlaybackState state;
long duration = -1;
synchronized (mLock) {
state = mPlaybackState;
if (mMetadata != null && mMetadata.containsKey(MediaMetadata.METADATA_KEY_DURATION)) {
duration = mMetadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
}
}
PlaybackState result = null;
if (state != null) {
if (state.getState() == PlaybackState.STATE_PLAYING
|| state.getState() == PlaybackState.STATE_FAST_FORWARDING
|| state.getState() == PlaybackState.STATE_REWINDING) {
long updateTime = state.getLastPositionUpdateTime();
long currentTime = SystemClock.elapsedRealtime();
if (updateTime > 0) {
long position = (long) (state.getPlaybackSpeed()
* (currentTime - updateTime)) + state.getPosition();
if (duration >= 0 && position > duration) {
position = duration;
} else if (position < 0) {
position = 0;
}
PlaybackState.Builder builder = new PlaybackState.Builder(state);
builder.setState(state.getState(), position, state.getPlaybackSpeed(),
currentTime);
result = builder.build();
}
}
}
return result == null ? state : result;
}
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());
}
@Override
public boolean onResourceReady(Object resource, Object model, Target target, boolean isFromMemoryCache, boolean isFirstResource) {
albumNow = (Bitmap) resource;
mediaSession.setMetadata(new MediaMetadata.Builder()
.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, albumNow)
.putString(MediaMetadata.METADATA_KEY_ARTIST, singerNow)
.putString(MediaMetadata.METADATA_KEY_TITLE, titleNow)
.build());
PlaybackState.Builder stateBuilder = new PlaybackState.Builder();
stateBuilder.setState(PlaybackState.STATE_PLAYING, mediaPlayer.getCurrentPosition(), 1.0f);
mediaSession.setPlaybackState(stateBuilder.build());
buildNotification(MyConstant.playing);
return false;
}
private void updatePlaybackState() {
if (mCallback == null) {
return;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
stateBuilder.setState(mState, getCurrentStreamPosition(), 1.0f, SystemClock.elapsedRealtime());
mCallback.onPlaybackStatusChanged(stateBuilder.build());
}
private void updatePlaybackState() {
if (mCallback == null) {
return;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
stateBuilder.setState(mState, getCurrentStreamPosition(), 1.0f, SystemClock.elapsedRealtime());
mCallback.onPlaybackStatusChanged(stateBuilder.build());
}
private void updatePlaybackState() {
long position = PlaybackState.PLAYBACK_POSITION_UNKNOWN;
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY_PAUSE
| PlaybackState.ACTION_SKIP_TO_PREVIOUS
| PlaybackState.ACTION_SKIP_TO_NEXT);
stateBuilder.setState(mediaPlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED, position, 1.0f);
mSession.setPlaybackState(stateBuilder.build());
}
private void updatePlaybackState() {
if (mCallback == null) {
return;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
stateBuilder.setState(mState, getCurrentStreamPosition(), 1.0f, SystemClock.elapsedRealtime());
mCallback.onPlaybackStatusChanged(stateBuilder.build());
}
private void updatePlaybackState() {
if (mCallback == null) {
return;
}
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
stateBuilder.setState(mState, getCurrentStreamPosition(), 1.0f, SystemClock.elapsedRealtime());
mCallback.onPlaybackStatusChanged(stateBuilder.build());
}
public static Object newInstance(int state, long position, long bufferedPosition,
float speed, long actions, CharSequence errorMessage, long updateTime) {
PlaybackState.Builder stateObj = new PlaybackState.Builder();
stateObj.setState(state, position, speed, updateTime);
stateObj.setBufferedPosition(bufferedPosition);
stateObj.setActions(actions);
stateObj.setErrorMessage(errorMessage);
return stateObj.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());
}
private void updatePlaybackState() {
PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
.setActions(getAvailableActions());
int state = PlaybackState.STATE_PLAYING;
if (mPlaybackState == LeanbackPlaybackState.PAUSED || mPlaybackState == LeanbackPlaybackState.IDLE) {
state = PlaybackState.STATE_PAUSED;
}
stateBuilder.setState(state, mVideoView.getCurrentPosition(), 1.0f);
mSession.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());
}
@Override
public void onCreate() {
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession = new MediaSession(this, "telegramAudioPlayer");
playbackState = new PlaybackState.Builder();
albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
placeholder.draw(new Canvas(albumArtPlaceholder));
mediaSession.setCallback(new MediaSession.Callback() {
@Override
public void onPlay() {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onPause() {
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onSkipToNext() {
MediaController.getInstance().playNextMessage();
}
@Override
public void onSkipToPrevious() {
MediaController.getInstance().playPreviousMessage();
}
@Override
public void onStop() {
//stopSelf();
}
});
mediaSession.setActive(true);
}
super.onCreate();
}
@Override
public void onCreate() {
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession = new MediaSession(this, "telegramAudioPlayer");
playbackState = new PlaybackState.Builder();
albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
placeholder.draw(new Canvas(albumArtPlaceholder));
mediaSession.setCallback(new MediaSession.Callback() {
@Override
public void onPlay() {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onPause() {
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onSkipToNext() {
MediaController.getInstance().playNextMessage();
}
@Override
public void onSkipToPrevious() {
MediaController.getInstance().playPreviousMessage();
}
@Override
public void onStop() {
//stopSelf();
}
});
mediaSession.setActive(true);
}
super.onCreate();
}
@Override
public void onCreate() {
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.httpFileDidLoad);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.fileDidLoad);
}
imageReceiver = new ImageReceiver(null);
imageReceiver.setDelegate((imageReceiver, set, thumb, memCache) -> {
if (set && !TextUtils.isEmpty(loadingFilePath)) {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null) {
createNotification(messageObject, true);
}
loadingFilePath = null;
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession = new MediaSession(this, "telegramAudioPlayer");
playbackState = new PlaybackState.Builder();
albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
placeholder.draw(new Canvas(albumArtPlaceholder));
mediaSession.setCallback(new MediaSession.Callback() {
@Override
public void onPlay() {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onPause() {
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onSkipToNext() {
MediaController.getInstance().playNextMessage();
}
@Override
public void onSkipToPrevious() {
MediaController.getInstance().playPreviousMessage();
}
@Override
public void onSeekTo(long pos) {
MessageObject object = MediaController.getInstance().getPlayingMessageObject();
if (object != null) {
MediaController.getInstance().seekToProgress(object, pos / 1000 / (float) object.getDuration());
updatePlaybackState(pos);
}
}
@Override
public void onStop() {
//stopSelf();
}
});
mediaSession.setActive(true);
}
registerReceiver(headsetPlugReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
super.onCreate();
}
@Override
public void onCreate() {
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.httpFileDidLoad);
NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.fileDidLoad);
}
imageReceiver = new ImageReceiver(null);
imageReceiver.setDelegate((imageReceiver, set, thumb, memCache) -> {
if (set && !TextUtils.isEmpty(loadingFilePath)) {
MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
if (messageObject != null) {
createNotification(messageObject, true);
}
loadingFilePath = null;
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mediaSession = new MediaSession(this, "telegramAudioPlayer");
playbackState = new PlaybackState.Builder();
albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
placeholder.draw(new Canvas(albumArtPlaceholder));
mediaSession.setCallback(new MediaSession.Callback() {
@Override
public void onPlay() {
MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onPause() {
MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
}
@Override
public void onSkipToNext() {
MediaController.getInstance().playNextMessage();
}
@Override
public void onSkipToPrevious() {
MediaController.getInstance().playPreviousMessage();
}
@Override
public void onSeekTo(long pos) {
MessageObject object = MediaController.getInstance().getPlayingMessageObject();
if (object != null) {
MediaController.getInstance().seekToProgress(object, pos / 1000 / (float) object.getDuration());
updatePlaybackState(pos);
}
}
@Override
public void onStop() {
//stopSelf();
}
});
mediaSession.setActive(true);
}
registerReceiver(headsetPlugReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
super.onCreate();
}