List of usage examples for android.support.v4.media.session PlaybackStateCompat STATE_PAUSED
int STATE_PAUSED
To view the source code for android.support.v4.media.session PlaybackStateCompat STATE_PAUSED.
Click Source Link
From source file:net.simno.klingar.playback.LocalPlayback.java
@Override public void play(Track track) { Timber.d("play %s", track); playOnFocusGain = true;/* w w w . j av a 2 s.com*/ tryToGetAudioFocus(); registerAudioNoisyReceiver(); boolean mediaHasChanged = !track.equals(currentTrack); if (mediaHasChanged) { currentPosition = 0; currentTrack = track; } if (state == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && exoPlayer != null) { configExoPlayerState(); } else { state = PlaybackStateCompat.STATE_STOPPED; relaxResources(false); // release everything except ExoPlayer createExoPlayerIfNeeded(); state = PlaybackStateCompat.STATE_BUFFERING; if (callback != null) { callback.onPlaybackStatusChanged(); } Uri uri = Uri.parse(track.source()); ExtractorMediaSource source = new ExtractorMediaSource(uri, dataSourceFactory, extractorsFactory, null, null); configWhenReady = true; exoPlayer.setPlayWhenReady(false); exoPlayer.prepare(source); // 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. wifiLock.acquire(); } }
From source file:org.runbuddy.tomahawk.views.PlaybackPanel.java
private void init() { mTextViewContainer = (FrameLayout) findViewById(R.id.textview_container); mPanelContainer = findViewById(R.id.panel_container); mStationContainer = findViewById(R.id.station_container); mStationContainerInner = findViewById(R.id.station_container_inner); mStationContainerInner.addOnLayoutChangeListener(mStationLayoutChangeListener); mArtistNameButton = (FrameLayout) mTextViewContainer.findViewById(R.id.artist_name_button); mArtistTextView = (TextView) mArtistNameButton.findViewById(R.id.artist_textview); mTrackTextView = (TextView) mTextViewContainer.findViewById(R.id.track_textview); mCompletionTimeTextView = (TextView) findViewById(R.id.completiontime_textview); mCurrentTimeTextView = (TextView) findViewById(R.id.currenttime_textview); mSeekTimeTextView = (TextView) findViewById(R.id.seektime_textview); mStationTextView = (TextView) findViewById(R.id.station_textview); mResolverImageView = (ImageView) findViewById(R.id.resolver_imageview); mPlayButton = (ImageView) findViewById(R.id.play_button); mPauseButton = (ImageView) findViewById(R.id.pause_button); mProgressBar = (ProgressBar) findViewById(R.id.progressbar); mProgressBarThumb = findViewById(R.id.progressbar_thumb); mPlayPauseButtonContainer = (FrameLayout) findViewById(R.id.circularprogressbar_container); mPlayPauseButton = (CircularProgressView) mPlayPauseButtonContainer.findViewById(R.id.circularprogressbar); mProgressBarUpdater = new ProgressBarUpdater(new ProgressBarUpdater.UpdateProgressRunnable() { @Override/* ww w. j a va 2 s . c o m*/ public void updateProgress(PlaybackStateCompat playbackState, long duration) { if (playbackState == null) { return; } long currentPosition = playbackState.getPosition(); if (playbackState.getState() != PlaybackStateCompat.STATE_PAUSED) { // Calculate the elapsed time between the last position update and now and unless // paused, we can assume (delta * speed) + current position is approximately the // latest position. This ensure that we do not repeatedly call the getPlaybackState() // on MediaControllerCompat. long timeDelta = SystemClock.elapsedRealtime() - playbackState.getLastPositionUpdateTime(); currentPosition += (int) timeDelta * playbackState.getPlaybackSpeed(); } mProgressBar.setProgress((int) ((float) currentPosition / duration * 10000)); mPlayPauseButton.setProgress((float) currentPosition / duration * 1000); mCurrentTimeTextView.setText(ViewUtils.durationToString(currentPosition)); } }); }
From source file:net.simno.klingar.playback.PlaybackManager.java
/** * Switch to a different Playback instance, maintaining all playback state, if possible. * * @param newPlayback switch to this playback *///from w ww .ja va2s . c o m void switchToPlayback(@NonNull Playback newPlayback, boolean resumePlaying) { Timber.d("switchToPlayback %s resume %s", newPlayback.getClass().getSimpleName(), resumePlaying); // Suspend the current one @State int oldState = playback.getState(); int position = playback.getCurrentStreamPosition(); Track currentMediaId = playback.getCurrentTrack(); playback.stop(false); newPlayback.setCallback(this); newPlayback.setCurrentStreamPosition(position < 0 ? 0 : position); newPlayback.setCurrentTrack(currentMediaId); newPlayback.start(); // Finally swap the instance playback = newPlayback; switch (oldState) { case PlaybackStateCompat.STATE_BUFFERING: case PlaybackStateCompat.STATE_CONNECTING: case PlaybackStateCompat.STATE_PAUSED: playback.pause(); break; case PlaybackStateCompat.STATE_PLAYING: Track currentQueueItem = queueManager.currentTrack(); if (resumePlaying && currentQueueItem != null) { playback.play(currentQueueItem); } else if (!resumePlaying) { playback.pause(); } else { playback.stop(true); } break; default: } }
From source file:rocks.stalin.android.app.playback.RemotePlayback.java
@Override public void play(QueueItem item) { mPlayOnFocusGain = true;/*from www. java2 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 == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mediaPlayer != null) { configMediaPlayerState(); } else { mState = PlaybackStateCompat.STATE_STOPPED; relaxResources(false); // release everything except MediaPlayer MediaMetadataCompat track = mMusicProvider .getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId())); String source = null; Uri uriSource = null; //noinspection ResourceType if (track.containsKey(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE)) { source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE); if (source != null) { source = source.replaceAll(" ", "%20"); // Escape spaces for URLs } } else { uriSource = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.parseLong(track.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID))); } try { createMediaPlayerIfNeeded(); mState = PlaybackStateCompat.STATE_BUFFERING; mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); if (uriSource != null) { mediaPlayer.setDataSource(mContext, uriSource); } else { mediaPlayer.setDataSource(source); // 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(); } // 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! mediaPlayer.prepareAsync(); 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.reallynourl.nourl.fmpfoldermusicplayer.ui.notifications.MediaNotification.java
private static void updateMediaSession(MediaSessionCompat mediaSession) { ExtendedFile currentFile = MediaManager.getInstance().getCurrentFile(); if (currentFile != null) { long validActions = PlaybackStateCompat.ACTION_STOP; if (MediaManager.getInstance().canPlay()) { validActions |= PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PAUSE; }//w w w.j a va2s . c o m if (MediaManager.getInstance().hasNext()) { validActions |= PlaybackStateCompat.ACTION_SKIP_TO_NEXT; } if (MediaManager.getInstance().hasPrevious()) { validActions |= PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS; } int playState = MediaManager.getInstance().isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; mediaSession.setMetadata(new MediaMetadataCompat.Builder() .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, currentFile.getParentFile().getName()) .putString(MediaMetadataCompat.METADATA_KEY_TITLE, currentFile.getNameWithoutExtension()) .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, MediaManager.getInstance().getDuration()) .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, MediaManager.getInstance().getPlaylist().getCurrentIndex() + 1) .putLong(MediaMetadataCompat.METADATA_KEY_NUM_TRACKS, MediaManager.getInstance().getPlaylist().size()) //.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap) .build()); mediaSession.setPlaybackState(new PlaybackStateCompat.Builder() .setState(playState, MediaManager.getInstance().getPosition(), 1.0f).setActions(validActions) .build()); } else { mediaSession.setActive(false); } }
From source file:com.devbrackets.android.exomedia.EMLockScreen.java
@PlaybackStateCompat.State private int getPlaybackState(boolean isPlaying) { return isPlaying ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED; }
From source file:github.popeen.dsub.util.compat.RemoteControlClientLP.java
@Override public void setPlaybackState(int state, int index, int queueSize) { PlaybackStateCompat.Builder builder = new PlaybackStateCompat.Builder(); int newState = PlaybackStateCompat.STATE_NONE; switch (state) { case RemoteControlClient.PLAYSTATE_PLAYING: newState = PlaybackStateCompat.STATE_PLAYING; break;//from w w w.j ava 2 s .c om case RemoteControlClient.PLAYSTATE_STOPPED: newState = PlaybackStateCompat.STATE_STOPPED; break; case RemoteControlClient.PLAYSTATE_PAUSED: newState = PlaybackStateCompat.STATE_PAUSED; break; case RemoteControlClient.PLAYSTATE_BUFFERING: newState = PlaybackStateCompat.STATE_BUFFERING; break; } long position = -1; if (state == RemoteControlClient.PLAYSTATE_PLAYING || state == RemoteControlClient.PLAYSTATE_PAUSED) { position = downloadService.getPlayerPosition(); } builder.setState(newState, position, 1.0f); DownloadFile downloadFile = downloadService.getCurrentPlaying(); Entry entry = null; boolean isSong = true; if (downloadFile != null) { entry = downloadFile.getSong(); isSong = entry.isSong(); } builder.setActions(getPlaybackActions(isSong, index, queueSize)); if (entry != null) { addCustomActions(entry, builder); builder.setActiveQueueItemId(entry.getId().hashCode()); } PlaybackStateCompat playbackState = builder.build(); mediaSession.setPlaybackState(playbackState); previousState = state; }
From source file:com.torrenttunes.android.ui.FullScreenPlayerActivity.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_full_player); initializeToolbar();//from www . j ava2 s. com getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(""); mBackgroundImage = (ImageView) findViewById(R.id.background_image); mPauseDrawable = ActivityCompat.getDrawable(this, R.drawable.ic_pause_white_48dp); mPlayDrawable = ActivityCompat.getDrawable(this, R.drawable.ic_play_arrow_white_48dp); mPlayPause = (ImageView) findViewById(R.id.imageView1); 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 = mMediaController.getTransportControls(); controls.skipToNext(); } }); mSkipPrev.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MediaControllerCompat.TransportControls controls = mMediaController.getTransportControls(); controls.skipToPrevious(); } }); mPlayPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PlaybackStateCompat state = mMediaController.getPlaybackState(); MediaControllerCompat.TransportControls controls = mMediaController.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(Utils.formatMillis(progress)); } @Override public void onStartTrackingTouch(SeekBar seekBar) { stopSeekbarUpdate(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { mMediaController.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), mMediaBrowserConnectionCallback, null); }
From source file:com.mylovemhz.simplay.MediaControlFragment.java
private void configureButtons(PlaybackStateCompat state) { if (state == null) { return;/*from w w w.ja v a 2s . co m*/ } boolean enablePlay = false; switch (state.getState()) { case PlaybackStateCompat.STATE_STOPPED: albumImage.setImageResource(R.drawable.ic_album); artistText.setText(""); titleText.setText(""); seekBar.setProgress(0); seekBar.setMax(1); case PlaybackStateCompat.STATE_PAUSED: handler.removeCallbacks(seekTickRunnable); enablePlay = true; break; case PlaybackStateCompat.STATE_PLAYING: handler.post(seekTickRunnable); break; case PlaybackStateCompat.STATE_ERROR: Log.e(TAG, "error playbackstate: " + state.getErrorMessage()); Toast.makeText(getActivity(), state.getErrorMessage(), Toast.LENGTH_LONG).show(); break; } if (enablePlay) { playPauseButton.setImageResource(playDrawableResource); } else { playPauseButton.setImageResource(pauseDrawableResource); } playPauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (getPlaybackState() != PlaybackStateCompat.STATE_PLAYING) { mediaController.getTransportControls().play(); } else { mediaController.getTransportControls().pause(); } } }); previousButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mediaController.getTransportControls().skipToPrevious(); } }); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mediaController.getTransportControls().skipToNext(); } }); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { mediaController.getTransportControls().seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); }
From source file:org.runbuddy.tomahawk.ui.fragments.starpage.SocialActionsFragment.java
/** * Called every time an item inside a ListView or GridView is clicked * * @param view the clicked view//from w w w. jav a2s . co m * @param o the Object which corresponds to the click */ @Override public void onItemClick(View view, Object o, Segment segment) { if (getMediaController() == null) { Log.e(TAG, "onItemClick failed because getMediaController() is null"); return; } final TomahawkMainActivity activity = (TomahawkMainActivity) getActivity(); Object item = o; if (o instanceof SocialAction) { item = ((SocialAction) o).getTargetObject(); } Bundle bundle = new Bundle(); if (item instanceof User) { bundle.putString(TomahawkFragment.USER, ((User) item).getId()); bundle.putInt(CONTENT_HEADER_MODE, ContentHeaderFragment.MODE_HEADER_STATIC_USER); FragmentUtils.replace(activity, UserPagerFragment.class, bundle); } else if (item instanceof Query && o instanceof SocialAction) { PlaylistEntry entry = mUser.getPlaylistEntry((SocialAction) o); if (entry.getQuery().isPlayable()) { if (getPlaybackManager().getCurrentEntry() == entry) { // if the user clicked on an already playing track int playState = getMediaController().getPlaybackState().getState(); if (playState == PlaybackStateCompat.STATE_PLAYING) { getMediaController().getTransportControls().pause(); } else if (playState == PlaybackStateCompat.STATE_PAUSED) { getMediaController().getTransportControls().play(); } } else { Playlist playlist; if (mShowMode == SHOW_MODE_SOCIALACTIONS) { playlist = mUser.getSocialActionsPlaylist(); } else { playlist = mUser.getFriendsFeedPlaylist(); } getPlaybackManager().setPlaylist(playlist, entry); getMediaController().getTransportControls().play(); } } } else if (item instanceof Album) { bundle.putString(TomahawkFragment.ALBUM, ((Album) item).getCacheKey()); bundle.putString(TomahawkFragment.COLLECTION_ID, mCollection.getId()); bundle.putInt(CONTENT_HEADER_MODE, ContentHeaderFragment.MODE_HEADER_DYNAMIC); FragmentUtils.replace(activity, PlaylistEntriesFragment.class, bundle); } else if (item instanceof Artist) { bundle.putString(TomahawkFragment.ARTIST, ((Artist) item).getCacheKey()); bundle.putString(TomahawkFragment.COLLECTION_ID, mCollection.getId()); bundle.putInt(CONTENT_HEADER_MODE, ContentHeaderFragment.MODE_HEADER_DYNAMIC_PAGER); bundle.putLong(CONTAINER_FRAGMENT_ID, IdGenerator.getSessionUniqueId()); FragmentUtils.replace(activity, ArtistPagerFragment.class, bundle); } else if (item instanceof Playlist) { bundle.putInt(CONTENT_HEADER_MODE, ContentHeaderFragment.MODE_HEADER_DYNAMIC); bundle.putString(TomahawkFragment.PLAYLIST, ((Playlist) item).getCacheKey()); FragmentUtils.replace(activity, PlaylistEntriesFragment.class, bundle); } }