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

The following examples show how to use android.media.session.PlaybackState#STATE_PAUSED . 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: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 2
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 6 votes vote down vote up
@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();
    }
}
 
Example 3
Source File: PlaybackManager.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
public void pause() {
    if (isPlaying()) {
        mMediaPlayer.pause();
        mAudioManager.abandonAudioFocus(this);
    }
    mState = PlaybackState.STATE_PAUSED;
    updatePlaybackState();
}
 
Example 4
Source File: PlaybackManager.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = PlaybackState.STATE_PLAYING;
                updatePlaybackState();
            }
            float volume = canDuck ? 0.2f : 1.0f;
            mMediaPlayer.setVolume(volume, volume);
        }
    } else if (mState == PlaybackState.STATE_PLAYING) {
        mMediaPlayer.pause();
        mState = PlaybackState.STATE_PAUSED;
        updatePlaybackState();
    }
}
 
Example 5
Source File: PlaybackManager.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
public void pause() {
    if (isPlaying()) {
        mMediaPlayer.pause();
        mAudioManager.abandonAudioFocus(this);
    }
    mState = PlaybackState.STATE_PAUSED;
    updatePlaybackState();
}
 
Example 6
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 7
Source File: PlaybackManager.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = PlaybackState.STATE_PLAYING;
                updatePlaybackState();
            }
            float volume = canDuck ? 0.2f : 1.0f;
            mMediaPlayer.setVolume(volume, volume);
        }
    } else if (mState == PlaybackState.STATE_PLAYING) {
        mMediaPlayer.pause();
        mState = PlaybackState.STATE_PAUSED;
        updatePlaybackState();
    }
}
 
Example 8
Source File: PlaybackManager.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
public void pause() {
    if (isPlaying()) {
        mMediaPlayer.pause();
        mAudioManager.abandonAudioFocus(this);
    }
    mState = PlaybackState.STATE_PAUSED;
    updatePlaybackState();
}
 
Example 9
Source File: MusicPlayerActivity.java    From io2015-codelabs with Apache License 2.0 5 votes vote down vote up
@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 */
    }
}
 
Example 10
Source File: PlaybackOverlayFragment.java    From BuildingForAndroidTV with MIT License 5 votes vote down vote up
private void next(boolean autoPlay) {
    if (++mCurrentItem >= mItems.size()) {
        mCurrentItem = 0;
    }
    Bundle bundle = new Bundle();
    bundle.putBoolean(PlaybackActivity.AUTO_PLAY, autoPlay);
    if (autoPlay) {
        mCurrentPlaybackState = PlaybackState.STATE_PAUSED;
    }
    mMediaController.getTransportControls().playFromMediaId(mItems.get(mCurrentItem).getId(), bundle);
    mFfwRwdSpeed = INITIAL_SPEED;
}
 
Example 11
Source File: PlaybackManager.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
/**
 * 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 = PlaybackState.STATE_PLAYING;
                updatePlaybackState();
            }
            float volume = canDuck ? 0.2f : 1.0f;
            mMediaPlayer.setVolume(volume, volume);
        }
    } else if (mState == PlaybackState.STATE_PLAYING) {
        mMediaPlayer.pause();
        mState = PlaybackState.STATE_PAUSED;
        updatePlaybackState();
    }
}
 
Example 12
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 13
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 14
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 15
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 16
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
@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 */
    }
}
 
Example 17
Source File: MusicPlayerActivity.java    From android-music-player with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 18
Source File: VideoFragment.java    From leanback-homescreen-channels with Apache License 2.0 5 votes vote down vote up
private void updatePlaybackState() {
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());
    int state = PlaybackStateCompat.STATE_PLAYING;
    if (!mMediaPlayerGlue.isPlaying()) {
        state = PlaybackState.STATE_PAUSED;
    }
    stateBuilder.setState(state, mMediaPlayerGlue.getCurrentPosition(), 1.0f);
    mSession.setPlaybackState(stateBuilder.build());
}
 
Example 19
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 20
Source File: PlaybackOverlayFragment.java    From BuildingForAndroidTV with MIT License 5 votes vote down vote up
private void prev(boolean autoPlay) {
    if (--mCurrentItem < 0) {
        mCurrentItem = mItems.size() - 1;
    }
    Bundle bundle = new Bundle();
    bundle.putBoolean(PlaybackActivity.AUTO_PLAY, autoPlay);
    if (autoPlay) {
        mCurrentPlaybackState = PlaybackState.STATE_PAUSED;
    }
    mMediaController.getTransportControls().playFromMediaId(mItems.get(mCurrentItem).getId(), bundle);
    mFfwRwdSpeed = INITIAL_SPEED;
}