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:org.chromium.chrome.browser.media.ui.NotificationMediaPlaybackControls.java

private void updateNotification() {
    if (mService == null)
        return;//from w ww.  j  av a  2 s.  c  o m

    if (mMediaNotificationInfo == null) {
        // Notification was hidden before we could update it.
        assert mNotificationBuilder == null;
        return;
    }

    // Android doesn't badge the icons for RemoteViews automatically when
    // running the app under the Work profile.
    if (mNotificationIcon == null) {
        Drawable notificationIconDrawable = ApiCompatibilityUtils.getUserBadgedIcon(mContext,
                R.drawable.audio_playing);
        mNotificationIcon = drawableToBitmap(notificationIconDrawable);
    }

    if (mNotificationBuilder == null) {
        mNotificationBuilder = new NotificationCompat.Builder(mContext).setSmallIcon(R.drawable.audio_playing)
                .setAutoCancel(false).setLocalOnly(true)
                .setDeleteIntent(mService.getPendingIntent(ListenerService.ACTION_STOP));
    }
    mNotificationBuilder.setOngoing(!mMediaNotificationInfo.isPaused);
    mNotificationBuilder.setContentIntent(createContentIntent());

    RemoteViews contentView = createContentView();

    contentView.setTextViewText(R.id.title, mMediaNotificationInfo.title);
    contentView.setTextViewText(R.id.status, getStatus());
    if (mNotificationIcon != null) {
        contentView.setImageViewBitmap(R.id.icon, mNotificationIcon);
    } else {
        contentView.setImageViewResource(R.id.icon, R.drawable.audio_playing);
    }

    if (mMediaNotificationInfo.isPaused) {
        contentView.setImageViewResource(R.id.playpause, R.drawable.ic_vidcontrol_play);
        contentView.setContentDescription(R.id.playpause, mPlayDescription);
        contentView.setOnClickPendingIntent(R.id.playpause,
                mService.getPendingIntent(ListenerService.ACTION_PLAY));
    } else {
        contentView.setImageViewResource(R.id.playpause, R.drawable.ic_vidcontrol_pause);
        contentView.setContentDescription(R.id.playpause, mPauseDescription);
        contentView.setOnClickPendingIntent(R.id.playpause,
                mService.getPendingIntent(ListenerService.ACTION_PAUSE));
    }

    mNotificationBuilder.setContent(contentView);
    mNotificationBuilder.setVisibility(mMediaNotificationInfo.isPrivate ? NotificationCompat.VISIBILITY_PRIVATE
            : NotificationCompat.VISIBILITY_PUBLIC);

    if (mMediaSession == null) {
        mMediaSession = new MediaSessionCompat(mContext, mContext.getString(R.string.app_name),
                new ComponentName(mContext.getPackageName(), MediaButtonReceiver.class.getName()), null);
        mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mMediaSession.setCallback(mMediaSessionCallback);
        mMediaSession.setActive(true);
    }

    mMediaSession.setMetadata(createMetadata());

    PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE);
    if (mMediaNotificationInfo.isPaused) {
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PAUSED,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    } else {
        playbackStateBuilder.setState(PlaybackStateCompat.STATE_PLAYING,
                PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1.0f);
    }
    mMediaSession.setPlaybackState(playbackStateBuilder.build());

    Notification notification = mNotificationBuilder.build();

    // We keep the service as a foreground service while the media is playing. When it is not,
    // the service isn't stopped but is no longer in foreground, thus at a lower priority.
    // While the service is in foreground, the associated notification can't be swipped away.
    // Moving it back to background allows the user to remove the notification.
    if (mMediaNotificationInfo.isPaused) {
        mService.stopForeground(false /* removeNotification */);

        NotificationManagerCompat manager = NotificationManagerCompat.from(mContext);
        manager.notify(R.id.media_playback_notification, notification);
    } else {
        mService.startForeground(R.id.media_playback_notification, notification);
    }
}

From source file:com.scooter1556.sms.android.activity.FullScreenPlayerActivity.java

private void updateProgress() {
    if (lastPlaybackState == null) {
        return;// www .  ja v a  2s  . co m
    }

    long currentPosition = lastPlaybackState.getPosition();

    if (lastPlaybackState.getState() != PlaybackStateCompat.STATE_PAUSED) {
        // Calculate the elapsed time between the last position update and now and unless
        // paused, we can assume (delta * speed) + current position is approximately the
        // latest position. This ensures that we do not repeatedly call the getPlaybackState()
        // on MediaControllerCompat.
        long timeDelta = SystemClock.elapsedRealtime() - lastPlaybackState.getLastPositionUpdateTime();
        currentPosition += (int) timeDelta * lastPlaybackState.getPlaybackSpeed();
    }

    seekbar.setProgress((int) currentPosition);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

private void startSeeking() {
    Log.d(TAG, "startSeeking()");

    if (playbackState != PlaybackStateCompat.STATE_PLAYING
            && playbackState != PlaybackStateCompat.STATE_PAUSED) {
        return;//from   ww w.  j  a v  a2  s  . c o m
    }

    if (playbackState == PlaybackStateCompat.STATE_PLAYING) {
        pause();
    }

    seekInProgress = true;
    setFadingEnabled(false);
    stopProgressAutomation();

    if (seekRunnable == null) {
        seekRunnable = new Runnable() {
            @Override
            public void run() {
                int currentTime = playbackControlsRow.getCurrentTime() + seekSpeed;

                if (currentTime < 0) {
                    currentPosition = 0;
                    playbackControlsRow.setCurrentTime(0);
                } else if (currentTime > playbackControlsRow.getTotalTime()) {
                    getActivity().finish();
                } else {
                    currentPosition = currentTime;
                    playbackControlsRow.setCurrentTime(currentTime);
                    seekHandler.postDelayed(this, DEFAULT_SEEK_DELAY);
                }
            }
        };

        seekHandler.postDelayed(seekRunnable, DEFAULT_SEEK_DELAY);
    }
}

From source file:org.runbuddy.tomahawk.services.PlaybackService.java

public void pause(boolean onAudioFocusLost) {
    Log.d(TAG, "pause");
    mPlayOnFocusGain = onAudioFocusLost;
    if (mAudioBecomingNoisyReceiver != null) {
        unregisterReceiver(mAudioBecomingNoisyReceiver);
        mAudioBecomingNoisyReceiver = null;
    }/* w w  w .  ja v  a  2  s.  co  m*/
    mSuicideHandler.start();
    mPluginServiceKillHandler.start();
    mScrobbleHandler.stop();
    mPlayState = PlaybackStateCompat.STATE_PAUSED;
    handlePlayState();
    updateMediaPlayState();
}

From source file:com.andryr.musicplayer.PlaybackService.java

private void updateMediaSession(String what) {

    if (!mMediaSession.isActive()) {
        mMediaSession.setActive(true);//from  www.  ja v  a  2s  . com
    }

    if (what.equals(PLAYSTATE_CHANGED)) {

        int playState = isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
        mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder()
                .setState(playState, getPlayerPosition(), 1.0F)
                .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE
                        | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                        | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
                .build());
    }

    if (what.equals(META_CHANGED)) {
        int largeArtSize = (int) getResources().getDimension(R.dimen.art_size);
        Bitmap artwork = ArtworkCache.getInstance().getCachedBitmap(getAlbumId(), largeArtSize, largeArtSize);

        MediaMetadataCompat.Builder builder = new MediaMetadataCompat.Builder()
                .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
                .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
                .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getSongTitle())
                .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, getTrackDuration())
                .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, artwork);
        mMediaSession.setMetadata(builder.build());

    }
}

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null || playbackControlsRow == null) {
        return;/*from   www.ja v  a  2 s  .c  om*/
    }

    lastPlaybackState = state;

    switch (state.getState()) {

    case PlaybackStateCompat.STATE_PLAYING:
        scheduleSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PAUSE);
        break;

    case PlaybackStateCompat.STATE_PAUSED:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        break;

    case PlaybackStateCompat.STATE_NONE:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        resetPlaybackRow();
        updatePlayListRow();
        break;

    case PlaybackStateCompat.STATE_STOPPED:
        stopSeekbarUpdate();
        playPauseAction.setIndex(PlayPauseAction.PLAY);
        playbackControlsRow.setCurrentTime(0);
        break;

    case PlaybackStateCompat.STATE_BUFFERING:
        stopSeekbarUpdate();
        break;

    default:
        Log.d(TAG, "Unhandled state: " + state.getState());
    }

    // Custom Actions
    for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
        switch (action.getAction()) {
        case MediaService.STATE_SHUFFLE_ON:
            shuffleAction.setIcon(
                    ContextCompat.getDrawable(getActivity(), R.drawable.ic_shuffle_enabled_white_48dp));

            // Update interface if necessary
            if (shuffleMode == null || !shuffleMode.equals(MediaService.STATE_SHUFFLE_ON)) {
                shuffleMode = MediaService.STATE_SHUFFLE_ON;
                updatePlayListRow();
            }

            break;

        case MediaService.STATE_SHUFFLE_OFF:
            shuffleAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_shuffle_white_48dp));

            // Update interface if necessary
            if (shuffleMode == null || !shuffleMode.equals(MediaService.STATE_SHUFFLE_OFF)) {
                shuffleMode = MediaService.STATE_SHUFFLE_OFF;
                updatePlayListRow();
            }

            break;

        case MediaService.STATE_REPEAT_NONE:
            repeatAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_white_48dp));
            break;

        case MediaService.STATE_REPEAT_ALL:
            repeatAction
                    .setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_enable_white_48dp));
            break;

        case MediaService.STATE_REPEAT_ONE:
            repeatAction.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_repeat_one_white_48dp));
            break;
        }
    }

    rowsAdapter.notifyArrayItemRangeChanged(rowsAdapter.indexOf(playbackControlsRow), 1);
}

From source file:com.wojtechnology.sunami.TheBrain.java

public void setProgressUI(int pos, boolean isPlaying) {
    int speed = isPlaying ? 1 : 0;
    int playState = isPlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED;
    PlaybackStateCompat state = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE
                    | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SEEK_TO
                    | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
            .setState(playState, pos, speed).build();
    MediaSessionCompatHelper.applyState(mSession, state);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

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

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

    if (mediaHasChanged) {
        currentPosition = 0;//from   w w  w. j ava2  s .  c o m
        currentMediaID = item.getDescription().getMediaId();
    }

    if (playbackState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mediaPlayer != null) {
        configMediaPlayerState();
    } else {
        playbackState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false);
        initialiseStream();
    }
}

From source file:com.scooter1556.sms.android.fragment.tv.TvVideoPlayerFragment.java

@Override
public void pause() {
    Log.d(TAG, "pause()");

    if (playbackState == PlaybackStateCompat.STATE_PLAYING) {
        // Pause media player and cancel the 'foreground service' state.
        if (mediaPlayer != null && mediaPlayer.getPlayWhenReady()) {
            mediaPlayer.setPlayWhenReady(false);
            currentPosition = mediaPlayer.getCurrentPosition();
            playbackState = PlaybackStateCompat.STATE_PAUSED;
        }// w w w . ja v  a  2s.co m

        // While paused, retain the Media Player but give up audio focus
        relaxResources(false);
    }

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

From source file:com.scooter1556.sms.lib.android.service.AudioPlayerService.java

public void pause() {
    if (player == null) {
        return;//from   w  ww . jav  a 2 s .c  o m
    }
    player.pause();
    isPaused = true;

    if (wakeLock.isHeld()) {
        wakeLock.release();
    }

    if (wifiLock.isHeld()) {
        wifiLock.release();
    }

    // Update state
    mediaState = PlaybackStateCompat.STATE_PAUSED;
    updatePlaybackState();

    // Update listeners
    for (AudioPlayerListener listener : listeners) {
        listener.PlaybackStateChanged();
    }
}