use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class MediaItemViewHolder method getStateFromController.
public static int getStateFromController(Context context) {
MediaControllerCompat controller = ((FragmentActivity) context).getSupportMediaController();
PlaybackStateCompat pbState = controller.getPlaybackState();
if (pbState == null || pbState.getState() == PlaybackStateCompat.STATE_ERROR) {
return MediaItemViewHolder.STATE_NONE;
} else if (pbState.getState() == PlaybackStateCompat.STATE_PLAYING) {
return MediaItemViewHolder.STATE_PLAYING;
} else {
return MediaItemViewHolder.STATE_PAUSED;
}
}
use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class PlaybackControlsFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_playback_controls, container, false);
mPlayPause = (ImageButton) rootView.findViewById(R.id.play_pause);
mPlayPause.setEnabled(true);
mPlayPause.setOnClickListener(mButtonListener);
mTitle = (TextView) rootView.findViewById(R.id.title);
mSubtitle = (TextView) rootView.findViewById(R.id.artist);
mExtraInfo = (TextView) rootView.findViewById(R.id.extra_info);
mAlbumArt = (ImageView) rootView.findViewById(R.id.album_art);
rootView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), FullScreenPlayerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController();
MediaMetadataCompat metadata = controller.getMetadata();
if (metadata != null) {
intent.putExtra(MusicPlayerActivity.EXTRA_CURRENT_MEDIA_DESCRIPTION, metadata.getDescription());
}
startActivity(intent);
}
});
return rootView;
}
use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class PlaybackControlsFragment method onPlaybackStateChanged.
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);
}
use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class PlaybackControlsFragment method onStart.
@Override
public void onStart() {
super.onStart();
LogHelper.d(TAG, "fragment.onStart");
MediaControllerCompat controller = ((FragmentActivity) getActivity()).getSupportMediaController();
if (controller != null) {
onConnected();
}
}
use of android.support.v4.media.session.MediaControllerCompat in project android-UniversalMusicPlayer by googlesamples.
the class BaseActivity method connectToSession.
private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
MediaControllerCompat mediaController = new MediaControllerCompat(this, token);
setSupportMediaController(mediaController);
mediaController.registerCallback(mMediaControllerCallback);
if (shouldShowControls()) {
showPlaybackControls();
} else {
LogHelper.d(TAG, "connectionCallback.onConnected: " + "hiding controls because metadata is null");
hidePlaybackControls();
}
if (mControlsFragment != null) {
mControlsFragment.onConnected();
}
onMediaControllerConnected();
}
Aggregations