Example usage for android.media.session PlaybackState STATE_PAUSED

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

Introduction

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

Prototype

int STATE_PAUSED

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

Click Source Link

Document

State indicating this item is currently paused.

Usage

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

@Override
public void pause() {
    try {/*w w w. j  a  v  a2s .c  o  m*/
        if (upnpPlayer.isPlaying()) {
            upnpPlayer.toPause();
            mCurrentPosition = (int) upnpPlayer.getCurrentMediaPosition();
        }
        mState = PlaybackState.STATE_PAUSED;
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
    } catch (Exception e) {
        LogHelper.e(TAG, e, "Exception pausing cast playback");
        if (mCallback != null) {
            mCallback.onError(e.getMessage());
        }
    }
}

From source file:hkapps.playmxtv.Activities.PlaybackOverlayActivity.java

private void updatePlaybackState(int position) {
    PlaybackState.Builder stateBuilder = new PlaybackState.Builder()
            .setActions(PlaybackState.ACTION_PLAY_PAUSE);
    int state = PlaybackState.STATE_PLAYING;
    if (mPlaybackState == LeanbackPlaybackState.PAUSED) {
        state = PlaybackState.STATE_PAUSED;
    }//from   w ww.j  a v  a 2 s .c  o m
    stateBuilder.setState(state, position, 1.0f);
    mSession.setPlaybackState(stateBuilder.build());
}

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

public void play(QueueItem item) {
    mPlayOnFocusGain = true;/* w  ww  .  ja  v  a  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
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackState.STATE_BUFFERING;

            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);

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

        } catch (IOException ex) {
            Log.e(TAG, "Exception playing song", ex);
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}

From source file:com.torrenttunes.android.LocalPlayback.java

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;//  w w w.  ja v  a 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
        MediaMetadataCompat track = mMusicProvider
                .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackState.STATE_BUFFERING;

            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);

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

        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}

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

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;//from w  w  w. j a  va  2  s.  co 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
        MediaMetadataCompat track = mMusicProvider.getMusic(item.getDescription().getMediaId());

        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackState.STATE_BUFFERING;

            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);
            mMediaPlayer.prepareAsync();

            mWifiLock.acquire();

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

        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}

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

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;//from ww w  .  j  a  v a 2 s.  co  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;// w ww. j ava2 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  .  java 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.app.LocalPlayback.java

@Override
public void pause() {
    if (mState == PlaybackState.STATE_PLAYING) {
        // Pause media player and cancel the 'foreground service' state.
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();// w  w w. j ava 2 s .c  o  m
            mCurrentPosition = mMediaPlayer.getCurrentPosition();
        }
        // while paused, retain the MediaPlayer but give up audio focus
        relaxResources(false);
        giveUpAudioFocus();
    }
    mState = PlaybackState.STATE_PAUSED;
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
    unregisterAudioNoisyReceiver();
}

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

public void pause() {
    if (mState == PlaybackState.STATE_PLAYING) {
        // Pause media player and cancel the 'foreground service' state.
        if (mMediaPlayer != null && mMediaPlayer.isPlaying()) {
            mMediaPlayer.pause();// www.j  a v  a2s  .  co  m
            mCurrentPosition = mMediaPlayer.getCurrentPosition();
        }
        // while paused, retain the MediaPlayer but give up audio focus
        relaxResources(false);
        giveUpAudioFocus();
    }
    mState = PlaybackState.STATE_PAUSED;
    if (mCallback != null) {
        mCallback.onPlaybackStatusChanged(mState);
    }
    unregisterAudioNoisyReceiver();
}