Example usage for android.support.v4.media.session PlaybackStateCompat STATE_PAUSED

List of usage examples for android.support.v4.media.session PlaybackStateCompat STATE_PAUSED

Introduction

In this page you can find the example usage for android.support.v4.media.session PlaybackStateCompat STATE_PAUSED.

Prototype

int STATE_PAUSED

To view the source code for android.support.v4.media.session PlaybackStateCompat STATE_PAUSED.

Click Source Link

Document

State indicating this item is currently paused.

Usage

From source file:com.example.android.supportv7.media.Player.java

protected void publishState(int state) {
    if (mMediaSession == null) {
        return;/* ww  w.ja va 2 s  . c o m*/
    }
    PlaybackStateCompat.Builder bob = new PlaybackStateCompat.Builder();
    bob.setActions(PLAYBACK_ACTIONS);
    switch (state) {
    case STATE_PLAYING:
        bob.setState(PlaybackStateCompat.STATE_PLAYING, -1, 1);
        break;
    case STATE_READY:
    case STATE_PAUSED:
        bob.setState(PlaybackStateCompat.STATE_PAUSED, -1, 0);
        break;
    case STATE_IDLE:
        bob.setState(PlaybackStateCompat.STATE_STOPPED, -1, 0);
        break;
    }
    PlaybackStateCompat pbState = bob.build();
    Log.d(TAG, "Setting state to " + pbState);
    mMediaSession.setPlaybackState(pbState);
    if (state != STATE_IDLE) {
        mMediaSession.setActive(true);
    } else {
        mMediaSession.setActive(false);
    }
}

From source file:com.doctoror.fuckoffmusicplayer.data.reporter.MediaSessionPlaybackReporter.java

@PlaybackStateCompat.State
private static int toPlaybackStateCompat(@NonNull final PlaybackState state) {
    switch (state) {
    case STATE_LOADING:
        return PlaybackStateCompat.STATE_BUFFERING;

    case STATE_PLAYING:
        return PlaybackStateCompat.STATE_PLAYING;

    case STATE_PAUSED:
        return PlaybackStateCompat.STATE_PAUSED;

    case STATE_ERROR:
        return PlaybackStateCompat.STATE_ERROR;

    case STATE_IDLE:
    default://www  . ja  v  a  2 s  .c  o m
        return PlaybackStateCompat.STATE_NONE;
    }
}

From source file:com.appdevper.mediaplayer.app.PlaybackManager.java

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 *//*www .j  av  a  2  s .  c  o m*/
public void updatePlaybackState(String error) {
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    //noinspection ResourceType
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    MediaSessionCompat.QueueItem currentMusic = mQueueManager.getCurrentMusic();
    if (currentMusic != null) {
        stateBuilder.setActiveQueueItemId(currentMusic.getQueueId());
    }

    mServiceCallback.onPlaybackStateUpdated(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mServiceCallback.onNotificationRequired();
    }
}

From source file:rocks.stalin.android.app.playback.PlaybackManager.java

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 */// w  w w  . j  a v a 2s.  c o  m
public void updatePlaybackState(String error) {
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(PlaybackStateCompat.ERROR_CODE_UNKNOWN_ERROR, error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    //noinspection ResourceType
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    MediaSessionCompat.QueueItem currentMusic = mQueueManager.getCurrentMusic();
    if (currentMusic != null) {
        stateBuilder.setActiveQueueItemId(currentMusic.getQueueId());
    }

    mServiceCallback.onPlaybackStateUpdated(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mServiceCallback.onNotificationRequired();
    }
}

From source file:com.murati.oszk.audiobook.playback.PlaybackManager.java

/**
 * Update the current media player state, optionally showing an error message.
 *
 * @param error if not null, error message to present to the user.
 */// w w w.  ja  va  2  s  .c om
public void updatePlaybackState(String error) {
    LogHelper.d(TAG, "updatePlaybackState, playback state=" + mPlayback.getState());
    long position = PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN;
    if (mPlayback != null && mPlayback.isConnected()) {
        position = mPlayback.getCurrentStreamPosition();
    }

    //noinspection ResourceType
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder()
            .setActions(getAvailableActions());

    setCustomAction(stateBuilder);
    int state = mPlayback.getState();

    // If there is an error message, send it to the playback state:
    if (error != null) {
        // Error states are really only supposed to be used for errors that cause playback to
        // stop unexpectedly and persist until the user takes action to fix it.
        stateBuilder.setErrorMessage(error);
        state = PlaybackStateCompat.STATE_ERROR;
    }
    //noinspection ResourceType
    stateBuilder.setState(state, position, 1.0f, SystemClock.elapsedRealtime());

    // Set the activeQueueItemId if the current index is valid.
    MediaSessionCompat.QueueItem currentMusic = mQueueManager.getCurrentTrack();
    if (currentMusic != null) {
        stateBuilder.setActiveQueueItemId(currentMusic.getQueueId());
    }

    mServiceCallback.onPlaybackStateUpdated(stateBuilder.build());

    if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_PAUSED) {
        mServiceCallback.onNotificationRequired();
    }
}

From source file:com.scooter1556.sms.android.playback.CastPlayback.java

@Override
public void play(MediaSessionCompat.QueueItem item) {
    Log.d(TAG, "play(" + item.getDescription().getMediaId() + ")");

    boolean mediaHasChanged = !TextUtils.equals(item.getDescription().getMediaId(), currentMediaId);

    if (mediaHasChanged) {
        currentMediaId = item.getDescription().getMediaId();
        currentPosition = 0;//from  w  ww. j  a v a2s  .c o m
    }

    if (playbackState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged
            && remoteMediaClient.hasMediaSession()) {
        remoteMediaClient.play();
    } else {
        loadMedia(item, true);
        playbackState = PlaybackStateCompat.STATE_BUFFERING;
    }

    if (callback != null) {
        callback.onPlaybackStatusChanged(playbackState);
    }
}

From source file:com.classiqo.nativeandroid_32bitz.playback.LocalPlayback.java

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;/*from   ww w .  ja va 2 s. co m*/

    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);

    if (mediaHasChanged) {
        mCurrentPosition = 0;
        mCurrentMediaId = mediaId;
    }

    if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false);
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        //noinspection ResourceType
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);

        if (source != null) {
            source = source.replaceAll(" ", "%20");
        }

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackStateCompat.STATE_BUFFERING;

            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);

            mMediaPlayer.prepareAsync();
            mWifiLock.acquire();

            if (mCallback != null) {
                mCallback.onPlaybackStatusChanged(mState);
            }
        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");

            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}

From source file:org.runbuddy.tomahawk.ui.fragments.PlaylistEntriesFragment.java

/**
 * Called every time an item inside a ListView or GridView is clicked
 *
 * @param view    the clicked view//  ww w . jav a 2s. c o m
 * @param item    the Object which corresponds to the click
 * @param segment
 */
@Override
public void onItemClick(View view, Object item, Segment segment) {
    if (getMediaController() == null) {
        Log.e(TAG, "onItemClick failed because getMediaController() is null");
        return;
    }
    if (item instanceof PlaylistEntry) {
        PlaylistEntry entry = (PlaylistEntry) item;
        if (entry.getQuery().isPlayable()) {
            if (getPlaybackManager().getCurrentEntry() == entry) {
                // if the user clicked on an already playing track
                int playState = getMediaController().getPlaybackState().getState();
                if (playState == PlaybackStateCompat.STATE_PLAYING) {
                    getMediaController().getTransportControls().pause();
                } else if (playState == PlaybackStateCompat.STATE_PAUSED) {
                    getMediaController().getTransportControls().play();
                }
            } else {
                getPlaybackManager().setPlaylist(mCurrentPlaylist, entry);
                getMediaController().getTransportControls().play();
            }
        }
    }
}

From source file:com.example.android.mediabrowserservice.Playback.java

public void play(MediaSessionCompat.QueueItem item) {
    mPlayOnFocusGain = true;//from w ww  .  ja  v  a 2s . c  o m
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentPosition = 0;
        mCurrentMediaId = mediaId;
    }

    if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackStateCompat.STATE_BUFFERING;

            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);

            // Starts preparing the media player in the background. When
            // it's done, it will call our OnPreparedListener (that is,
            // the onPrepared() method on this class, since we set the
            // listener to 'this'). Until the media player is prepared,
            // we *cannot* call start() on it!
            mMediaPlayer.prepareAsync();

            // If we are streaming from the internet, we want to hold a
            // Wifi lock, which prevents the Wifi radio from going to
            // sleep while the song is playing.
            mWifiLock.acquire();

            if (mCallback != null) {
                mCallback.onPlaybackStatusChanged(mState);
            }

        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}

From source file:com.example.lzhang.stockchartt.media.playback.LocalPlayback.java

@Override
public void play(MediaSessionCompat.QueueItem item) {
    mPlayOnFocusGain = true;/*from  www  .  ja  va2s.c  o m*/
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentPosition = 0;
        mCurrentMediaId = mediaId;
    }

    if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        //            MediaMetadataCompat track = mMusicProvider.getMusic(
        //                    MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        //noinspection ResourceType
        String source = item.getDescription().getMediaUri().toString();//track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
        }

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackStateCompat.STATE_BUFFERING;

            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);

            // Starts preparing the media player in the background. When
            // it's done, it will call our OnPreparedListener (that is,
            // the onPrepared() method on this class, since we set the
            // listener to 'this'). Until the media player is prepared,
            // we *cannot* call start() on it!
            mMediaPlayer.prepareAsync();

            // If we are streaming from the internet, we want to hold a
            // Wifi lock, which prevents the Wifi radio from going to
            // sleep while the song is playing.
            mWifiLock.acquire();

            if (mCallback != null) {
                mCallback.onPlaybackStatusChanged(mState);
            }

        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}