Example usage for android.media.session PlaybackState STATE_PLAYING

List of usage examples for android.media.session PlaybackState STATE_PLAYING

Introduction

In this page you can find the example usage for android.media.session PlaybackState STATE_PLAYING.

Prototype

int STATE_PLAYING

To view the source code for android.media.session PlaybackState STATE_PLAYING.

Click Source Link

Document

State indicating this item is currently playing.

Usage

From source file:com.example.android.supportv4.media.Playback.java

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link android.media.AudioManager.OnAudioFocusChangeListener}
 *///from  w w  w  .j  a va2  s. c o  m
@Override
public void onAudioFocusChange(int focusChange) {
    Log.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 == PlaybackState.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 {
        Log.e(TAG, "onAudioFocusChange: Ignoring unsupported focusChange: " + focusChange);
    }
    configMediaPlayerState();
}

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

/**
 * Called by AudioManager on audio focus changes.
 * Implementation of {@link AudioManager.OnAudioFocusChangeListener}
 *///w  ww .  j  a  v  a  2 s  . 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 == PlaybackState.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.example.android.supportv4.media.Playback.java

/**
 * Called when MediaPlayer has completed a seek
 *
 * @see android.media.MediaPlayer.OnSeekCompleteListener
 *///from   w  w w  . ja va  2  s  .c  om
@Override
public void onSeekComplete(MediaPlayer mp) {
    Log.d(TAG, "onSeekComplete from MediaPlayer:" + mp.getCurrentPosition());
    mCurrentPosition = mp.getCurrentPosition();
    if (mState == PlaybackState.STATE_BUFFERING) {
        mMediaPlayer.start();
        mState = PlaybackState.STATE_PLAYING;
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

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

/**
 * Called when MediaPlayer has completed a seek
 *
 * @see OnSeekCompleteListener//  w  ww. 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 == PlaybackState.STATE_BUFFERING) {
        mMediaPlayer.start();
        mState = PlaybackState.STATE_PLAYING;
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

From source file:de.kraenksoft.c3tv.ui.PlaybackOverlayFragment.java

private void setupCallbacks() {

    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override//  w  w  w  . j  a va2s.  co  m
        public boolean onError(MediaPlayer mp, int what, int extra) {
            mVideoView.stopPlayback();
            setPlaybackState(PlaybackState.STATE_STOPPED);
            return false;
        }
    });

    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            if (getPlaybackState() == PlaybackState.STATE_PLAYING) {
                mVideoView.start();
            }
        }
    });

    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            setPlaybackState(PlaybackState.STATE_STOPPED);
        }
    });

}

From source file:com.appdevper.mediaplayer.activity.FullScreenPlayerActivity.java

private void updatePlaybackState(PlaybackStateCompat state) {
    if (state == null) {
        return;// w w w  .  j a v  a  2 s  . c om
    }
    mLastPlaybackState = state;
    if (getSupportMediaController() != null && getSupportMediaController().getExtras() != null) {
        String castName = getSupportMediaController().getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        String line3Text = castName == null ? ""
                : getResources().getString(R.string.casting_to_device, castName);
        mLine3.setText(line3Text);
    }

    switch (state.getState()) {
    case PlaybackState.STATE_PLAYING:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPauseDrawable);
        mControllers.setVisibility(VISIBLE);
        scheduleSeekbarUpdate();
        break;
    case PlaybackState.STATE_PAUSED:
        mControllers.setVisibility(VISIBLE);
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackState.STATE_NONE:
    case PlaybackState.STATE_STOPPED:
        mLoading.setVisibility(INVISIBLE);
        mPlayPause.setVisibility(VISIBLE);
        mPlayPause.setImageDrawable(mPlayDrawable);
        stopSeekbarUpdate();
        break;
    case PlaybackState.STATE_BUFFERING:
        mPlayPause.setVisibility(INVISIBLE);
        mLoading.setVisibility(VISIBLE);
        mLine3.setText(R.string.loading);
        stopSeekbarUpdate();
        break;
    default:
        LogHelper.d(TAG, "Unhandled state ", state.getState());
    }

    mSkipNext
            .setVisibility((state.getActions() & PlaybackState.ACTION_SKIP_TO_NEXT) == 0 ? INVISIBLE : VISIBLE);
    mSkipPrev.setVisibility(
            (state.getActions() & PlaybackState.ACTION_SKIP_TO_PREVIOUS) == 0 ? INVISIBLE : VISIBLE);
}

From source file:org.mythtv.android.presentation.view.fragment.TvPlaybackOverlayFragment.java

private void setupCallbacks() {

    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override//from  ww w .j  a  va 2 s.com
        public boolean onError(MediaPlayer mp, int what, int extra) {

            mVideoView.stopPlayback();
            setPlaybackState(PlaybackState.STATE_STOPPED);

            return false;
        }

    });

    mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            if (getPlaybackState() == PlaybackState.STATE_PLAYING) {
                mVideoView.start();
            }
        }
    });

    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {

            setPlaybackState(PlaybackState.STATE_STOPPED);

        }

    });

}

From source file:com.aengbee.android.leanback.ui.PlaybackOverlayCustomFragment.java

private long getAvailableActions(int nextState) {
    long actions = PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID
            | PlaybackState.ACTION_PLAY_FROM_SEARCH | PlaybackState.ACTION_SKIP_TO_NEXT
            | PlaybackState.ACTION_SKIP_TO_PREVIOUS | PlaybackState.ACTION_FAST_FORWARD
            | PlaybackState.ACTION_REWIND | PlaybackState.ACTION_PAUSE;

    if (nextState == PlaybackState.STATE_PLAYING) {
        actions |= PlaybackState.ACTION_PAUSE;
    }//from  w  ww.  j av  a 2s . co m

    return actions;
}

From source file:com.aengbee.android.leanback.ui.PlaybackOverlayCustomFragment.java

private void play() {
    // Request audio focus whenever we resume playback
    // because the app might have abandoned audio focus due to the AUDIOFOCUS_LOSS.
    requestAudioFocus();/*  w w w .  j  a  va 2s .c  om*/

    if (mPlayer == null) {
        setPlaybackState(PlaybackState.STATE_NONE);
        return;
    }
    if (!mGlue.isMediaPlaying()) {
        mPlayer.getPlayerControl().start();
        setPlaybackState(PlaybackState.STATE_PLAYING);
    }
}

From source file:com.koma.music.service.MusicService.java

private void updateMediaSession(final String what) {
    LogUtils.i(TAG, "updateMediaSession what : " + what);
    int playState = mIsSupposedToBePlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_PAUSED;

    long playBackStateActions = PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE
            | PlaybackState.ACTION_PLAY_FROM_MEDIA_ID | PlaybackState.ACTION_PAUSE
            | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS
            | PlaybackState.ACTION_STOP;

    if (what.equals(MusicServiceConstants.PLAYSTATE_CHANGED)
            || what.equals(MusicServiceConstants.POSITION_CHANGED)) {
        mSession.setPlaybackState(new PlaybackState.Builder().setActions(playBackStateActions)
                .setActiveQueueItemId(getAudioId()).setState(playState, position(), 1.0f).build());
    } else if (what.equals(META_CHANGED) || what.equals(MusicServiceConstants.QUEUE_CHANGED)) {
        LogUtils.i(TAG, "sadsadsadsad thread id : " + Thread.currentThread().getId() + "name : "
                + Thread.currentThread().getName());
        /*if (albumArt != null) {
        // RemoteControlClient wants to recycle the bitmaps thrown at it, so we need
        // to make sure not to hand out our cache copy
        Bitmap.Config config = albumArt.getConfig();
        if (config == null) {//from  w w  w  . ja  va  2 s.  c o m
            config = Bitmap.Config.ARGB_8888;
        }
        albumArt = albumArt.copy(config, false);
        }*/

        /*mSession.setMetadata(new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, getArtistName())
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, getAlbumArtistName())
            .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, getAlbumName())
            .putString(MediaMetadataCompat.METADATA_KEY_TITLE, getTrackName())
            .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration())
            .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getQueuePosition() + 1)
            .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, getQueue().length)
            .putString(MediaMetadataCompat.METADATA_KEY_GENRE, getGenreName())
            .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART,
                    mShowAlbumArtOnLockscreen ? albumArt : null)
            .build());*/

        if (what.equals(MusicServiceConstants.QUEUE_CHANGED)) {
            updateMediaSessionQueue();
        }

        mSession.setPlaybackState(new PlaybackState.Builder().setActions(playBackStateActions)
                .setActiveQueueItemId(getAudioId()).setState(playState, position(), 1.0f).build());
    }
    LogUtils.i(TAG, "updateMediaSession finished");
}