Java Code Examples for android.media.session.PlaybackState#Builder

The following examples show how to use android.media.session.PlaybackState#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClementineMediaSessionV21.java    From Android-Remote with GNU General Public License v3.0 6 votes vote down vote up
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());
}
 
Example 2
Source File: VideoPlayerActivity.java    From iview-android-tv with MIT License 6 votes vote down vote up
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());
}
 
Example 3
Source File: MediaServiceHandler.java    From Aerlink-for-Android with MIT License 5 votes vote down vote up
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());
}
 
Example 4
Source File: MusicBrowserService.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 5
Source File: PlaybackActivity.java    From BuildingForAndroidTV with MIT License 5 votes vote down vote up
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());
}
 
Example 6
Source File: MusicBrowserService.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 7
Source File: PlaybackStateCompatApi21.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 8
Source File: PlaybackManager.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 9
Source File: PlaybackManager.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 10
Source File: MediaSessionRecord.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 11
Source File: PlaybackManager.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 12
Source File: PlaybackManager.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 13
Source File: PlayService.java    From music_player with Open Software License 3.0 5 votes vote down vote up
@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;
}
 
Example 14
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 15
Source File: MusicBrowserService.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example 16
Source File: MusicPlayerService.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@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();
}
 
Example 17
Source File: MusicPlayerService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@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();
}
 
Example 18
Source File: MusicPlayerService.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@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();
}
 
Example 19
Source File: MusicPlayerService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@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();
}