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:nuclei.media.playback.FallbackPlayback.java

@Override
protected void internalPlay(MediaMetadata metadataCompat, Timing timing, boolean seek) {
    mPlayOnFocusGain = true;/*w w w.j  a v a 2s. com*/
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    boolean mediaHasChanged = mCurrentMediaId == null
            || !TextUtils.equals(metadataCompat.getDescription().getMediaId(), mCurrentMediaId.toString());
    if (mediaHasChanged) {
        mCurrentPosition = getStartStreamPosition();
        mMetadata = metadataCompat;
        mCurrentMediaId = MediaProvider.getInstance().getMediaId(metadataCompat.getDescription().getMediaId());
    }

    if (mState == PlaybackStateCompat.STATE_PLAYING && !mediaHasChanged && mMediaPlayer != null
            && mMediaPlayer.isPlaying()) {
        if (mCallback != null) {
            mCallback.onPlaybackStatusChanged(mState);
        }
    } else if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        //noinspection ResourceType
        String source = metadataCompat.getString(MediaProvider.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackStateCompat.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 (Exception ex) {
            if (mCallback != null) {
                mCallback.onError(ex, true);
            }
        }
    }

    if (timing != null && seek)
        internalSeekTo(timing.start);
}

From source file:com.scooter1556.sms.android.activity.FullScreenPlayerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen_player);
    initialiseToolbar();/*www.j av  a 2  s  .  c  om*/
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("");
    }

    backgroundImage = (ImageView) findViewById(R.id.background_image);
    pauseDrawable = ContextCompat.getDrawable(this, R.drawable.ic_pause_white_48dp);
    playDrawable = ContextCompat.getDrawable(this, R.drawable.ic_play_arrow_white_48dp);
    playPause = (ImageView) findViewById(R.id.play_pause);
    skipNext = (ImageView) findViewById(R.id.next);
    skipPrev = (ImageView) findViewById(R.id.prev);
    shuffle = (ImageView) findViewById(R.id.shuffle);
    repeat = (ImageView) findViewById(R.id.repeat);
    start = (TextView) findViewById(R.id.startText);
    end = (TextView) findViewById(R.id.endText);
    seekbar = (SeekBar) findViewById(R.id.seekBar);
    title = (TextView) findViewById(R.id.title);
    subtitle = (TextView) findViewById(R.id.subtitle);
    extra = (TextView) findViewById(R.id.extra);
    loading = (ProgressBar) findViewById(R.id.progressBar);
    controllers = findViewById(R.id.controllers);

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

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

    playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = mediaController.getPlaybackState();
            if (state != null) {
                MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();
                switch (state.getState()) {
                case PlaybackStateCompat.STATE_PLAYING: // fall through
                case PlaybackStateCompat.STATE_BUFFERING:
                    controls.pause();
                    stopSeekbarUpdate();
                    break;
                case PlaybackStateCompat.STATE_PAUSED:
                case PlaybackStateCompat.STATE_STOPPED:
                    controls.play();
                    scheduleSeekbarUpdate();
                    break;
                }
            }
        }
    });

    shuffle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = mediaController.getPlaybackState();
            MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

            if (state == null) {
                return;
            }

            for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
                switch (action.getAction()) {
                case MediaService.STATE_SHUFFLE_ON:
                    controls.sendCustomAction(MediaService.STATE_SHUFFLE_ON, null);
                    break;

                case MediaService.STATE_SHUFFLE_OFF:
                    controls.sendCustomAction(MediaService.STATE_SHUFFLE_OFF, null);
                    break;
                }
            }

            updatePlaybackState(state);
        }
    });

    repeat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = mediaController.getPlaybackState();
            MediaControllerCompat.TransportControls controls = mediaController.getTransportControls();

            if (state == null) {
                return;
            }

            for (PlaybackStateCompat.CustomAction action : state.getCustomActions()) {
                switch (action.getAction()) {
                case MediaService.STATE_REPEAT_NONE:
                case MediaService.STATE_REPEAT_ALL:
                case MediaService.STATE_REPEAT_ONE:
                    controls.sendCustomAction(action.getAction(), null);
                    break;
                }
            }

            updatePlaybackState(state);
        }
    });

    seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            start.setText(DateUtils.formatElapsedTime(progress / 1000));
        }

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

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

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

    mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MediaService.class), connectionCallback,
            null);
}

From source file:android.support.v17.leanback.media.MediaControllerAdapter.java

@Override
public boolean isPlaying() {
    if (mController.getPlaybackState() == null) {
        return false;
    }/*from w w  w . j av a 2 s  .co  m*/
    return mController.getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING
            || mController.getPlaybackState().getState() == PlaybackStateCompat.STATE_FAST_FORWARDING
            || mController.getPlaybackState().getState() == PlaybackStateCompat.STATE_REWINDING;
}

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

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

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

private void handlePlayState() {
    if (mPreparedQuery != null) {
        if (mPlayState == PlaybackStateCompat.STATE_PAUSED && getMediaPlayerInstance().isPlaying()) {
            getMediaPlayerInstance().pause();
        } else if (mPlayState == PlaybackStateCompat.STATE_PLAYING && !getMediaPlayerInstance().isPlaying()) {
            getMediaPlayerInstance().play();
        }/* ww w. jav  a2 s. com*/
    }
}

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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_player);
    initializeToolbar();//from  w  ww  .  j a va  2s  .c om
    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 = MediaControllerCompat
                    .getMediaController(FullScreenPlayerActivity.this).getTransportControls();
            controls.skipToNext();
        }
    });

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

    mPlayPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PlaybackStateCompat state = MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this)
                    .getPlaybackState();
            if (state != null) {
                MediaControllerCompat.TransportControls controls = MediaControllerCompat
                        .getMediaController(FullScreenPlayerActivity.this).getTransportControls();
                switch (state.getState()) {
                case PlaybackStateCompat.STATE_PLAYING: // fall through
                case PlaybackStateCompat.STATE_BUFFERING:
                    controls.pause();
                    stopSeekbarUpdate();
                    break;
                case PlaybackStateCompat.STATE_PAUSED:
                case PlaybackStateCompat.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(DateUtils.formatElapsedTime(progress / 1000));
        }

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

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            MediaControllerCompat.getMediaController(FullScreenPlayerActivity.this).getTransportControls()
                    .seekTo(seekBar.getProgress());
            scheduleSeekbarUpdate();
        }
    });

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

    mMediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MusicService.class),
            mConnectionCallback, null);
}

From source file:com.scooter1556.sms.android.fragment.tv.TvAudioPlayerFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    view.setOnKeyListener(new View.OnKeyListener() {
        @Override//  w ww .  j av  a2  s  . c  om
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
                    if (lastPlaybackState != null) {
                        if (lastPlaybackState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                            mediaController.getTransportControls().pause();
                        } else {
                            mediaController.getTransportControls().play();
                        }
                    }

                    return true;
                }
            }
            return false;
        }
    });
}

From source file:org.runbuddy.tomahawk.views.PlaybackPanel.java

public void setup(final boolean isPanelExpanded) {
    mInitialized = true;/*from  ww w  . ja va2  s.  c  o m*/

    mPlayPauseButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mMediaController != null) {
                int playState = mMediaController.getPlaybackState().getState();
                if (playState == PlaybackStateCompat.STATE_PAUSED
                        || playState == PlaybackStateCompat.STATE_NONE) {
                    mMediaController.getTransportControls().play();
                } else if (playState == PlaybackStateCompat.STATE_PLAYING) {
                    mMediaController.getTransportControls().pause();
                    mMediaController.getTransportControls()
                            .sendCustomAction(PlaybackService.ACTION_STOP_NOTIFICATION, null);
                }
            }
        }
    });

    mPlayPauseButton.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            PreferenceUtils.edit().putBoolean(PreferenceUtils.COACHMARK_SEEK_DISABLED, true).apply();
            View coachMark = ViewUtils.ensureInflation(PlaybackPanel.this,
                    R.id.playbackpanel_seek_coachmark_stub, R.id.playbackpanel_seek_coachmark);
            coachMark.setVisibility(GONE);
            if (!isPanelExpanded || getResources().getBoolean(R.bool.is_landscape)) {
                AnimationUtils.fade(mTextViewContainer, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false, true);
            }
            AnimationUtils.fade(mPlayPauseButtonContainer, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false,
                    true);
            AnimationUtils.fade(mResolverImageView, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false, true);
            AnimationUtils.fade(mCompletionTimeTextView, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false, true);
            AnimationUtils.fade(mProgressBarThumb, AnimationUtils.DURATION_PLAYBACKSEEKMODE, true, true);
            AnimationUtils.fade(mCurrentTimeTextView, AnimationUtils.DURATION_PLAYBACKSEEKMODE, true, true);
            AnimationUtils.fade(mSeekTimeTextView, AnimationUtils.DURATION_PLAYBACKSEEKMODE, true, true);
            AnimationUtils.fade(mProgressBar, AnimationUtils.DURATION_PLAYBACKSEEKMODE, true, true);

            mPlayPauseButton.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (!isPanelExpanded || getResources().getBoolean(R.bool.is_landscape)) {
                            AnimationUtils.fade(mTextViewContainer, AnimationUtils.DURATION_PLAYBACKSEEKMODE,
                                    true, true);
                        }
                        AnimationUtils.fade(mPlayPauseButtonContainer, AnimationUtils.DURATION_PLAYBACKSEEKMODE,
                                true, true);
                        AnimationUtils.fade(mResolverImageView, AnimationUtils.DURATION_PLAYBACKSEEKMODE, true,
                                true);
                        AnimationUtils.fade(mCompletionTimeTextView, AnimationUtils.DURATION_PLAYBACKSEEKMODE,
                                true, true);
                        AnimationUtils.fade(mProgressBarThumb, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false,
                                true);
                        AnimationUtils.fade(mCurrentTimeTextView, AnimationUtils.DURATION_PLAYBACKSEEKMODE,
                                false, true);
                        AnimationUtils.fade(mSeekTimeTextView, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false,
                                true);
                        AnimationUtils.fade(mProgressBar, AnimationUtils.DURATION_PLAYBACKSEEKMODE, false,
                                true);
                        mPlayPauseButton.setOnTouchListener(null);
                        if (!mAbortSeeking) {
                            int seekTime = (int) ((mLastThumbPosition - mProgressBar.getX())
                                    / mProgressBar.getWidth() * mCurrentDuration);
                            mMediaController.getTransportControls().seekTo(seekTime);
                        }
                    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                        float eventX = event.getX();
                        float progressBarX = mProgressBar.getX();
                        float finalX;
                        if (eventX > mProgressBar.getWidth() + progressBarX) {
                            // Only fade out thumb if eventX is above the threshold
                            int threshold = getResources()
                                    .getDimensionPixelSize(R.dimen.playback_panel_seekbar_threshold_end);
                            mAbortSeeking = eventX > mProgressBar.getWidth() + progressBarX + threshold;
                            finalX = mProgressBar.getWidth() + progressBarX;
                        } else if (eventX < progressBarX) {
                            // Only fade out thumb if eventX is below the threshold
                            int threshold = getResources()
                                    .getDimensionPixelSize(R.dimen.playback_panel_seekbar_threshold_start);
                            mAbortSeeking = eventX < progressBarX - threshold;
                            finalX = progressBarX;
                        } else {
                            mAbortSeeking = false;
                            finalX = eventX;
                        }
                        if (mAbortSeeking) {
                            AnimationUtils.fade(mProgressBarThumb,
                                    AnimationUtils.DURATION_PLAYBACKSEEKMODE_ABORT, false, true);
                        } else {
                            AnimationUtils.fade(mProgressBarThumb,
                                    AnimationUtils.DURATION_PLAYBACKSEEKMODE_ABORT, true, true);
                        }
                        mLastThumbPosition = finalX;
                        mProgressBarThumb.setX(finalX);
                        int seekTime = (int) ((finalX - mProgressBar.getX()) / mProgressBar.getWidth()
                                * mCurrentDuration);
                        mSeekTimeTextView.setText(ViewUtils.durationToString(seekTime));
                    }
                    return false;
                }
            });
            return true;
        }
    });

    setupAnimations();
}

From source file:com.example.android.uamp.playback.LocalPlayback.java

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

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

private boolean isStatePlaying() {
    return isMediaPlayerPlaying() || mState == PlaybackStateCompat.STATE_PLAYING
            || mState == PlaybackStateCompat.STATE_BUFFERING || mState == PlaybackStateCompat.STATE_CONNECTING;
}