private void pauseMedia() { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); if (controller != null) { controller.getTransportControls().pause(); } }
public void onConnected() { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); LogHelper.d(TAG, "onConnected, mediaController==null? ", controller == null); if (controller != null) { onMetadataChanged(controller.getMetadata()); onPlaybackStateChanged(controller.getPlaybackState()); controller.registerCallback(mCallback); } }
@Override public void onStop() { super.onStop(); LogHelper.d(TAG, "fragment.onStop"); MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); if (controller != null) { controller.unregisterCallback(mCallback); } }
/** * Extracts any available {@link KeyEvent} from an {@link Intent#ACTION_MEDIA_BUTTON} intent, * passing it onto the {@link MediaSessionCompat} using {@link * MediaControllerCompat#dispatchMediaButtonEvent(KeyEvent)}, which in turn will trigger callbacks * to the {@link MediaSessionCompat.Callback} registered via {@link * MediaSessionCompat#setCallback(MediaSessionCompat.Callback)}. * * <p>The returned {@link KeyEvent} is non-null if any {@link KeyEvent} is found and can be used * if any additional processing is needed beyond what is done in the {@link * MediaSessionCompat.Callback}. An example of is to prevent redelivery of a {@link * KeyEvent#KEYCODE_MEDIA_PLAY_PAUSE} Intent in the case of the Service being restarted (which, by * default, will redeliver the last received Intent). * * <pre> * KeyEvent keyEvent = MediaButtonReceiver.handleIntent(mediaSession, intent); * if (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) { * Intent emptyIntent = new Intent(intent); * emptyIntent.setAction(""); * startService(emptyIntent); * } * </pre> * * @param mediaSessionCompat A {@link MediaSessionCompat} that has a {@link * MediaSessionCompat.Callback} set. * @param intent The intent to parse. * @return The extracted {@link KeyEvent} if found, or null. */ public static KeyEvent handleIntent(MediaSessionCompat mediaSessionCompat, Intent intent) { if (mediaSessionCompat == null || intent == null || !Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()) || !intent.hasExtra(Intent.EXTRA_KEY_EVENT)) { return null; } KeyEvent ke = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); MediaControllerCompat mediaController = mediaSessionCompat.getController(); mediaController.dispatchMediaButtonEvent(ke); return ke; }
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; } if (state == null) { return; } boolean enablePlay = false; switch (state.getState()) { case PlaybackStateCompat.STATE_PAUSED: case PlaybackStateCompat.STATE_STOPPED: enablePlay = true; break; case PlaybackStateCompat.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); }
@Override public void onClick(View v) { MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController(); PlaybackStateCompat stateObj = controller.getPlaybackState(); final int state = stateObj == null ? PlaybackStateCompat.STATE_NONE : stateObj.getState(); LogHelper.d(TAG, "Button pressed, in state " + state); switch (v.getId()) { case R.id.play_pause: LogHelper.d(TAG, "Play button pressed, in state " + state); if (state == PlaybackStateCompat.STATE_PAUSED || state == PlaybackStateCompat.STATE_STOPPED || state == PlaybackStateCompat.STATE_NONE) { playMedia(); } else if (state == PlaybackStateCompat.STATE_PLAYING || state == PlaybackStateCompat.STATE_BUFFERING || state == PlaybackStateCompat.STATE_CONNECTING) { pauseMedia(); } break; } }