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

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

Introduction

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

Prototype

int STATE_PLAYING

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

Click Source Link

Document

State indicating this item is currently playing.

Usage

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

/**
 * Called when MediaPlayer has completed a seek
 *//*from   w w w. ja v  a2s  . c o  m*/
@Override
public void onSeekComplete(MediaPlayer mp) {
    LogHelper.d(TAG, "onSeekComplete from MediaPlayer:", mp.getCurrentPosition());
    mCurrentPosition = mp.getCurrentPosition();
    if (mState == PlaybackStateCompat.STATE_BUFFERING) {
        registerAudioNoisyReceiver();
        mediaPlayer.start();
        mState = PlaybackStateCompat.STATE_PLAYING;
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

From source file:com.bayapps.android.robophish.playback.LocalPlayback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
 *//* w ww .  j  a  v  a 2s  . c o m*/
@Override
public void onAudioFocusChange(int focusChange) {
    LogHelper.d(TAG, "onAudioFocusChange. focusChange=", focusChange);
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // We have gained focus:
        mAudioFocus = AUDIO_FOCUSED;

    } 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.
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        // If we are playing, we need to reset media player by calling configMediaPlayerState
        // with mAudioFocus properly set.
        if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
            // If we don't have audio focus and can't duck, we save the information that
            // we were playing, so that we can resume playback once we get the focus back.
            mPlayOnFocusGain = true;
        }
    } else {
        LogHelper.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: ", focusChange);
    }
    configMediaPlayerState();
}

From source file:com.scooter1556.sms.android.manager.MediaNotificationManager.java

private void setNotificationPlaybackState(NotificationCompat.Builder builder) {
    if (playbackState == null || !started) {
        mediaService.stopForeground(true);
        return;//from w w  w.j  a  v  a 2s  .  c  o m
    }

    // Make sure that the notification can be dismissed by the user when we are not playing:
    builder.setOngoing(playbackState.getState() == PlaybackStateCompat.STATE_PLAYING);
}

From source file:org.runbuddy.tomahawk.mediaplayers.PluginMediaPlayer.java

private void handlePlayState() {
    if (mPreparedQuery != null) {
        if (mPlayState == PlaybackStateCompat.STATE_PAUSED && mIsPlaying) {
            callService(MSG_PAUSE);/*from w w  w  .j  a  va2  s.  c  o m*/
        } else if (mPlayState == PlaybackStateCompat.STATE_PLAYING && !mIsPlaying) {
            callService(MSG_PLAY);
        }
    }
}

From source file:org.runbuddy.tomahawk.utils.MediaNotification.java

private void updateNotificationPlaybackState() {
    Log.d(TAG, "updateNotificationPlaybackState. mPlaybackState=" + mPlaybackState);
    if (mPlaybackState == null || !mStarted) {
        Log.d(TAG, "updateNotificationPlaybackState. cancelling notification!");
        mService.stopForeground(true);//from w  w w . j av  a2  s . c o  m
        return;
    }
    if (mNotificationBuilder == null) {
        Log.d(TAG, "updateNotificationPlaybackState. there is no notificationBuilder. "
                + "Ignoring request to update state!");
        return;
    }
    if (mPlaybackState.getPosition() >= 0 && mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        Log.d(TAG, "updateNotificationPlaybackState. updating playback position to "
                + (System.currentTimeMillis() - mPlaybackState.getPosition()) / 1000 + " seconds");
        mNotificationBuilder.setWhen(System.currentTimeMillis() - mPlaybackState.getPosition())
                .setShowWhen(true).setUsesChronometer(true);
    } else {
        Log.d(TAG, "updateNotificationPlaybackState. hiding playback position");
        mNotificationBuilder.setWhen(0).setShowWhen(false).setUsesChronometer(false);
    }

    updatePlayPauseAction();

    // Make sure that the notification can be dismissed by the user when we are not playing:
    mNotificationBuilder.setOngoing(mPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING);
    if (mPlaybackState.getState() != PlaybackStateCompat.STATE_PLAYING) {
        mService.stopForeground(false);
    }
}

From source file:com.murati.oszk.audiobook.ui.FullScreenPlayerActivity.java

private void updateProgress() {
    if (mLastPlaybackState == null) {
        return;/*from w w w .  j  av a2 s.com*/
    }

    //TODO: fix current position
    long currentPosition = mLastPlaybackState.getPosition();
    if (mLastPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
        // 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 ensure that we do not repeatedly call the getPlaybackState()
        // on MediaControllerCompat.
        long timeDelta = SystemClock.elapsedRealtime() - mLastPlaybackState.getLastPositionUpdateTime();
        currentPosition += (long) timeDelta * mLastPlaybackState.getPlaybackSpeed();
    }
    mSeekbar.setProgress((int) currentPosition);

    try {
        if (mSeekbar.getMax() < currentPosition) {
            mSeekbar.setMax((int) currentPosition + 30000);
            mEnd.setText(DateUtils.formatElapsedTime(mSeekbar.getMax() / 1000));
        }
    } catch (Exception ex) {
        //TODO: fix length detection
    }
}

From source file:nuclei.media.playback.FallbackPlayback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *//*from w  ww.  ja v  a 2  s  . c  o  m*/
@Override
public void onAudioFocusChange(int focusChange) {
    if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
        // We have gained focus:
        mAudioFocus = AUDIO_FOCUSED;

    } 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.
        boolean canDuck = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK;
        mAudioFocus = canDuck ? AUDIO_NO_FOCUS_CAN_DUCK : AUDIO_NO_FOCUS_NO_DUCK;

        // If we are playing, we need to reset media player by calling configMediaPlayerState
        // with mAudioFocus properly set.
        if (mState == PlaybackStateCompat.STATE_PLAYING && !canDuck) {
            // If we don't have audio focus and can't duck, we save the information that
            // we were playing, so that we can resume playback once we get the focus back.
            mPlayOnFocusGain = true;
        }
    }
    configMediaPlayerState();
}

From source file:com.bayapps.android.robophish.playback.LocalPlayback.java

/**
 * Called when MediaPlayer has completed a seek
 *
 * @see OnSeekCompleteListener/*from   w w w . j  a  v  a 2s .co m*/
 */
@Override
public void onSeekComplete(MediaPlayer mp) {
    LogHelper.d(TAG, "onSeekComplete from MediaPlayer:", mp.getCurrentPosition());
    mCurrentPosition = mp.getCurrentPosition();
    if (mState == PlaybackStateCompat.STATE_BUFFERING) {
        mMediaPlayer.start();
        mState = PlaybackStateCompat.STATE_PLAYING;
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

From source file:nuclei.media.playback.CastPlayback.java

void updatePlaybackState() {
    final int status = VideoCastManager.getInstance().getPlaybackStatus();

    // Convert the remote playback states to media playback states.
    switch (status) {
    case MediaStatus.PLAYER_STATE_IDLE:
        final int idleReason = VideoCastManager.getInstance().getIdleReason();
        switch (idleReason) {
        case MediaStatus.IDLE_REASON_ERROR:
            if (mCallback != null)
                mCallback.onError(new Exception("Error: " + idleReason), true);
            break;
        case MediaStatus.IDLE_REASON_INTERRUPTED:
        case MediaStatus.IDLE_REASON_CANCELED:
            // TODO: What should happen here?
            mState = PlaybackStateCompat.STATE_NONE;
            if (mCallback != null)
                mCallback.onPlaybackStatusChanged(mState);
            break;
        case MediaStatus.IDLE_REASON_FINISHED:
            if (mCallback != null)
                mCallback.onCompletion();
            break;
        default:/*  w  w  w .ja va2s.  c  o  m*/
            setMetadataFromRemote();
            if (mCallback != null)
                mCallback.onPlaybackStatusChanged(mState);
            break;
        }
        break;
    case MediaStatus.PLAYER_STATE_BUFFERING:
        mState = PlaybackStateCompat.STATE_BUFFERING;
        setMetadataFromRemote();
        if (mCallback != null)
            mCallback.onPlaybackStatusChanged(mState);
        break;
    case MediaStatus.PLAYER_STATE_PLAYING:
        mState = PlaybackStateCompat.STATE_PLAYING;
        setMetadataFromRemote();
        if (mCallback != null)
            mCallback.onPlaybackStatusChanged(mState);
        break;
    case MediaStatus.PLAYER_STATE_PAUSED:
        mState = PlaybackStateCompat.STATE_PAUSED;
        setMetadataFromRemote();
        if (mCallback != null)
            mCallback.onPlaybackStatusChanged(mState);
        break;
    default: // case unknown
        setMetadataFromRemote();
        if (mCallback != null)
            mCallback.onPlaybackStatusChanged(mState);
        break;
    }
}

From source file:nuclei.media.playback.FallbackPlayback.java

/**
 * Called when MediaPlayer has completed a seek.
 *
 * @see OnSeekCompleteListener/*  ww w. java 2 s  .c  o m*/
 */
@Override
public void onSeekComplete(MediaPlayer mp) {
    mCurrentPosition = mp.getCurrentPosition();
    if (mState == PlaybackStateCompat.STATE_BUFFERING) {
        if (!mMediaPlayer.isPlaying())
            mMediaPlayer.start();
        mState = PlaybackStateCompat.STATE_PLAYING;
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}