Example usage for android.media.session PlaybackState STATE_STOPPED

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

Introduction

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

Prototype

int STATE_STOPPED

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

Click Source Link

Document

State indicating this item is currently stopped.

Usage

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

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;/*from   w  w w. java 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 == 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;//from  w w w. j  av  a2  s  .com
    }
    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.activity.FullScreenPlayerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_player);
    initializeToolbar();/*from   w w w  .  j a  v a  2 s  .c  o  m*/
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
    }

    mBackgroundImage = (ImageView) findViewById(R.id.background_image);
    mPauseDrawable = ContextCompat.getDrawable(this, R.drawable.uamp_ic_pause_white_48dp);
    mPlayDrawable = ContextCompat.getDrawable(this, R.drawable.uamp_ic_play_arrow_white_48dp);
    mPlayPause = (ImageView) findViewById(R.id.play_pause);
    mSkipNext = (ImageView) findViewById(R.id.next);
    mSkipPrev = (ImageView) findViewById(R.id.prev);
    mStart = (TextView) findViewById(R.id.startText);
    mEnd = (TextView) findViewById(R.id.endText);
    mSeekbar = (SeekBar) findViewById(R.id.seekBar1);
    mLine1 = (TextView) findViewById(R.id.line1);
    mLine2 = (TextView) findViewById(R.id.line2);
    mLine3 = (TextView) findViewById(R.id.line3);
    mLoading = (ProgressBar) findViewById(R.id.progressBar1);
    mControllers = findViewById(R.id.controllers);

    mSkipNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaControllerCompat.TransportControls controls = getSupportMediaController()
                    .getTransportControls();
            controls.skipToNext();
        }
    });

    mSkipPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MediaControllerCompat.TransportControls controls = getSupportMediaController()
                    .getTransportControls();
            controls.skipToPrevious();
        }
    });

    mPlayPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = getSupportMediaController().getPlaybackState();
            if (state != null) {
                MediaControllerCompat.TransportControls controls = getSupportMediaController()
                        .getTransportControls();
                switch (state.getState()) {
                case PlaybackState.STATE_PLAYING: // fall through
                case PlaybackState.STATE_BUFFERING:
                    controls.pause();
                    stopSeekbarUpdate();
                    break;
                case PlaybackState.STATE_PAUSED:
                case PlaybackState.STATE_STOPPED:
                    controls.play();
                    scheduleSeekbarUpdate();
                    break;
                default:
                    LogHelper.d(TAG, "onClick with state ", state.getState());
                }
            }
        }
    });

    mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            mStart.setText(Utils.formatMillis(progress));
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            stopSeekbarUpdate();
        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            getSupportMediaController().getTransportControls().seekTo(seekBar.getProgress());
            scheduleSeekbarUpdate();
        }
    });

    // Only update from the intent if we are not recreating from a config change:
    if (savedInstanceState == null) {
        updateFromParams(getIntent());
    }

}

From source file:com.appdevper.mediaplayer.ui.PlaybackControlsFragment.java

private void onPlaybackStateChanged(PlaybackStateCompat state) {
    LogHelper.d(TAG, "onPlaybackStateChanged ", state);
    if (getActivity() == null) {
        LogHelper.w(TAG, "onPlaybackStateChanged called when getActivity null,"
                + "this should not happen if the callback was properly unregistered. Ignoring.");
        return;//w w w . j av a2 s  . c  o m
    }
    if (state == null) {
        return;
    }
    boolean enablePlay = false;
    switch (state.getState()) {
    case PlaybackState.STATE_PAUSED:
    case PlaybackState.STATE_STOPPED:
        enablePlay = true;
        break;
    case PlaybackState.STATE_ERROR:
        LogHelper.e(TAG, "error playbackstate: ", state.getErrorMessage());
        Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show();
        break;
    }

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

    MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController();
    String extraInfo = null;
    if (controller != null && controller.getExtras() != null) {
        String castName = controller.getExtras().getString(MusicService.EXTRA_CONNECTED_CAST);
        if (castName != null) {
            extraInfo = getResources().getString(R.string.casting_to_device, castName);
        }
    }
    setExtraInfo(extraInfo);
}

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

private void setupCallbacks() {

    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override/*from   w  w  w . j  av a2  s. 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:org.mythtv.android.presentation.view.fragment.TvPlaybackOverlayFragment.java

private void setupCallbacks() {

    mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override/*from w w w  .  j a v a2s .  c  o 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;//from   www  .  j av  a  2s . 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:com.android.tv.MainActivity.java

private void setMediaSessionPlaybackState(boolean isPlaying) {
    PlaybackState.Builder builder = new PlaybackState.Builder();
    builder.setState(isPlaying ? PlaybackState.STATE_PLAYING : PlaybackState.STATE_STOPPED,
            PlaybackState.PLAYBACK_POSITION_UNKNOWN,
            isPlaying ? MEDIA_SESSION_PLAYING_SPEED : MEDIA_SESSION_STOPPED_SPEED);
    mMediaSession.setPlaybackState(builder.build());
}