Example usage for android.media.session PlaybackState STATE_BUFFERING

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

Introduction

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

Prototype

int STATE_BUFFERING

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

Click Source Link

Document

State indicating this item is currently buffering and will begin playing when enough data has buffered.

Usage

From source file:robert843.o2.pl.player.LocalPlayback.java

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

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

        String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE);
        // Asynchronously load the music catalog in a separate thread
        new AsyncTask<Void, Void, MovieParser>() {
            @Override
            protected MovieParser doInBackground(Void... params) {
                MovieParser parser = new MovieParser(track.getString("id"));
                try {
                    parser.parse();

                } catch (ParserConfigurationException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return parser;
            }

            @Override
            protected void onPostExecute(MovieParser current) {
                createMediaPlayerIfNeeded();

                mState = PlaybackState.STATE_BUFFERING;

                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mMediaPlayer.setDataSource(current.getUrl());
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // 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);
                }

            }
        }.execute();

    }
}

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

private void onPlaybackStateChanged(PlaybackStateCompat state) {
    LogHelper.d(TAG, "onPlaybackStateChanged ", state);
    if (state == null) {
        return;/*ww  w  . j a va 2  s . c om*/
    }
    mQueueAdapter.setActiveQueueItemId(state.getActiveQueueItemId());
    mQueueAdapter.notifyDataSetChanged();
    boolean enablePlay = false;
    StringBuilder statusBuilder = new StringBuilder();
    switch (state.getState()) {
    case PlaybackState.STATE_PLAYING:
        statusBuilder.append("playing");
        enablePlay = false;
        break;
    case PlaybackState.STATE_PAUSED:
        statusBuilder.append("paused");
        enablePlay = true;
        break;
    case PlaybackState.STATE_STOPPED:
        statusBuilder.append("ended");
        enablePlay = true;
        break;
    case PlaybackState.STATE_ERROR:
        statusBuilder.append("error: ").append(state.getErrorMessage());
        break;
    case PlaybackState.STATE_BUFFERING:
        statusBuilder.append("buffering");
        break;
    case PlaybackState.STATE_NONE:
        statusBuilder.append("none");
        enablePlay = false;
        break;
    case PlaybackState.STATE_CONNECTING:
        statusBuilder.append("connecting");
        break;
    default:
        statusBuilder.append(mPlaybackState);
    }
    statusBuilder.append(" -- At position: ").append(state.getPosition());
    LogHelper.d(TAG, statusBuilder.toString());

    if (enablePlay) {
        mPlayPause.setImageDrawable(
                ContextCompat.getDrawable(getActivity(), R.drawable.ic_play_arrow_white_24dp));
    } else {
        mPlayPause.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.ic_pause_white_24dp));
    }

    mSkipPrevious.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0);
    mSkipNext.setEnabled((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0);

    LogHelper.d(TAG, "Queue From MediaController *** Title " + mMediaController.getQueueTitle() + "\n: Queue: "
            + mMediaController.getQueue() + "\n Metadata " + mMediaController.getMetadata());
}

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

@Override
public void seekTo(int position) {
    LogHelper.d(TAG, "seekTo called with ", position);

    if (mMediaPlayer == null) {
        // If we do not have a current media player, simply update the current position
        mCurrentPosition = position;// w w w . ja v a2  s . c o  m
    } else {
        if (mMediaPlayer.isPlaying()) {
            mState = PlaybackState.STATE_BUFFERING;
        }
        mMediaPlayer.seekTo(position);
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
    }
}

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

public void seekTo(int position) {
    Log.d(TAG, "seekTo called with " + position);

    if (mMediaPlayer == null) {
        // If we do not have a current media player, simply update the current position
        mCurrentPosition = position;//from   w w w  . j a  v a 2 s . c o  m
    } else {
        if (mMediaPlayer.isPlaying()) {
            mState = PlaybackState.STATE_BUFFERING;
        }
        mMediaPlayer.seekTo(position);
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
    }
}

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

private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
    MediaControllerCompat mediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token);
    if (mediaController.getMetadata() == null) {
        finish();/*w w w. jav  a2 s.  c  o  m*/
        return;
    }
    setSupportMediaController(mediaController);
    mediaController.registerCallback(mCallback);
    PlaybackStateCompat state = mediaController.getPlaybackState();
    updatePlaybackState(state);
    MediaMetadataCompat metadata = mediaController.getMetadata();
    if (metadata != null) {
        updateMediaDescription(metadata.getDescription());
        updateDuration(metadata);
    }
    updateProgress();
    if (state != null && (state.getState() == PlaybackState.STATE_PLAYING
            || state.getState() == PlaybackState.STATE_BUFFERING)) {
        scheduleSeekbarUpdate();
    }
}

From source file:com.orangesoft.jook.CastPlayback.java

private void updatePlaybackState() {
    int status = castManager.getPlaybackStatus();
    int idleReason = castManager.getIdleReason();

    Log.d(TAG, "onRemoteMediaPlayerStatusUpdated " + status);

    // Convert the remote playback states to media playback states.
    switch (status) {
    case MediaStatus.PLAYER_STATE_IDLE:
        if (idleReason == MediaStatus.IDLE_REASON_FINISHED) {
            if (callback != null)
                callback.onCompletion();
        }/*  w  w w  .j  a  va  2 s.co m*/
        break;
    case MediaStatus.PLAYER_STATE_BUFFERING:
        safeStatusChangeNotification(PlaybackState.STATE_BUFFERING);
        break;
    case MediaStatus.PLAYER_STATE_PLAYING:
        safeStatusChangeNotification(PlaybackState.STATE_PLAYING);
        break;
    case MediaStatus.PLAYER_STATE_PAUSED:
        safeStatusChangeNotification(PlaybackState.STATE_PAUSED);
        break;
    }
}

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

private void updatePlaybackState() {
    int status = mCastManager.getPlaybackStatus();
    int idleReason = mCastManager.getIdleReason();

    LogHelper.d(TAG, "onRemoteMediaPlayerStatusUpdated ", status);

    // Convert the remote playback states to media playback states.
    switch (status) {
    case MediaStatus.PLAYER_STATE_IDLE:
        if (idleReason == MediaStatus.IDLE_REASON_FINISHED) {
            if (mCallback != null) {
                mCallback.onCompletion();
            }/* w ww  . ja  v  a2 s.  c o m*/
        }
        break;
    case MediaStatus.PLAYER_STATE_BUFFERING:
        mState = PlaybackState.STATE_BUFFERING;
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    case MediaStatus.PLAYER_STATE_PLAYING:
        mState = PlaybackState.STATE_PLAYING;
        updateMetadata();
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    case MediaStatus.PLAYER_STATE_PAUSED:
        mState = PlaybackState.STATE_PAUSED;
        updateMetadata();
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
        break;
    default: // case unknown
        LogHelper.d(TAG, "State default : ", status);
        break;
    }
}

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

/**
 * Reconfigures MediaPlayer according to audio focus settings and
 * starts/restarts it. This method starts/restarts the MediaPlayer
 * respecting the current audio focus state. So if we have focus, it will
 * play normally; if we don't have focus, it will either leave the
 * MediaPlayer paused or set it to a low volume, depending on what is
 * allowed by the current focus settings. This method assumes mPlayer !=
 * null, so if you are calling it, you have to do so from a context where
 * you are sure this is the case./*from  w  w  w . j a  va  2  s .c  o  m*/
 */
private void configMediaPlayerState() {
    Log.d(TAG, "configMediaPlayerState. mAudioFocus=" + mAudioFocus);
    if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
        // If we don't have audio focus and can't duck, we have to pause,
        if (mState == PlaybackState.STATE_PLAYING) {
            pause();
        }
    } else { // we have audio focus:
        if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
            mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
        } else {
            if (mMediaPlayer != null) {
                mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
            } // else do something for remote client.
        }
        // If we were playing when we lost focus, we need to resume playing.
        if (mPlayOnFocusGain) {
            if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
                Log.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to " + mCurrentPosition);
                if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
                    mMediaPlayer.start();
                    mState = PlaybackState.STATE_PLAYING;
                } else {
                    mMediaPlayer.seekTo(mCurrentPosition);
                    mState = PlaybackState.STATE_BUFFERING;
                }
            }
            mPlayOnFocusGain = false;
        }
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}

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

@Override
public void onSeekComplete(MediaPlayer mp) {
    LogHelper.d(TAG, "onSeekComplete from MediaPlayer: ", mp.getCurrentPosition());

    mCurrentPosition = mp.getCurrentPosition();

    if (mState == PlaybackState.STATE_BUFFERING) {
        registerAudioNoisyReceiver();//from w  ww.j  a v a  2  s. c om
        mMediaPlayer.start();
        mState = PlaybackState.STATE_PLAYING;
    }

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

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

/**
 * Reconfigures MediaPlayer according to audio focus settings and
 * starts/restarts it. This method starts/restarts the MediaPlayer
 * respecting the current audio focus state. So if we have focus, it will
 * play normally; if we don't have focus, it will either leave the
 * MediaPlayer paused or set it to a low volume, depending on what is
 * allowed by the current focus settings. This method assumes mPlayer !=
 * null, so if you are calling it, you have to do so from a context where
 * you are sure this is the case./*from   ww w  .java2s . c  o  m*/
 */
private void configMediaPlayerState() {
    LogHelper.d(TAG, "configMediaPlayerState. mAudioFocus=", mAudioFocus);
    if (mAudioFocus == AUDIO_NO_FOCUS_NO_DUCK) {
        // If we don't have audio focus and can't duck, we have to pause,
        if (mState == PlaybackState.STATE_PLAYING) {
            pause();
        }
    } else { // we have audio focus:
        if (mAudioFocus == AUDIO_NO_FOCUS_CAN_DUCK) {
            mMediaPlayer.setVolume(VOLUME_DUCK, VOLUME_DUCK); // we'll be relatively quiet
        } else {
            if (mMediaPlayer != null) {
                mMediaPlayer.setVolume(VOLUME_NORMAL, VOLUME_NORMAL); // we can be loud again
            } // else do something for remote client.
        }
        // If we were playing when we lost focus, we need to resume playing.
        if (mPlayOnFocusGain) {
            if (mMediaPlayer != null && !mMediaPlayer.isPlaying()) {
                LogHelper.d(TAG, "configMediaPlayerState startMediaPlayer. seeking to ", mCurrentPosition);
                if (mCurrentPosition == mMediaPlayer.getCurrentPosition()) {
                    mMediaPlayer.start();
                    mState = PlaybackState.STATE_PLAYING;
                } else {
                    mMediaPlayer.seekTo(mCurrentPosition);
                    mState = PlaybackState.STATE_BUFFERING;
                }
            }
            mPlayOnFocusGain = false;
        }
    }
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
}