Search in sources :

Example 6 with MediaControllerCompat

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;
    }
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 7 with MediaControllerCompat

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;
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Intent(android.content.Intent) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) ImageView(android.widget.ImageView) TextView(android.widget.TextView) View(android.view.View)

Example 8 with MediaControllerCompat

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);
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 9 with MediaControllerCompat

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();
    }
}
Also used : FragmentActivity(android.support.v4.app.FragmentActivity) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 10 with MediaControllerCompat

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();
}
Also used : MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Aggregations

MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)20 FragmentActivity (android.support.v4.app.FragmentActivity)10 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)3 Intent (android.content.Intent)2 ListRow (android.support.v17.leanback.widget.ListRow)2 ListRowPresenter (android.support.v17.leanback.widget.ListRowPresenter)2 MediaBrowserCompat (android.support.v4.media.MediaBrowserCompat)2 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)2 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)2 View (android.view.View)2 AbstractDetailsDescriptionPresenter (android.support.v17.leanback.widget.AbstractDetailsDescriptionPresenter)1 Action (android.support.v17.leanback.widget.Action)1 HeaderItem (android.support.v17.leanback.widget.HeaderItem)1 OnActionClickedListener (android.support.v17.leanback.widget.OnActionClickedListener)1 OnItemViewClickedListener (android.support.v17.leanback.widget.OnItemViewClickedListener)1 PlaybackControlsRow (android.support.v17.leanback.widget.PlaybackControlsRow)1 PlayPauseAction (android.support.v17.leanback.widget.PlaybackControlsRow.PlayPauseAction)1 SkipNextAction (android.support.v17.leanback.widget.PlaybackControlsRow.SkipNextAction)1 SkipPreviousAction (android.support.v17.leanback.widget.PlaybackControlsRow.SkipPreviousAction)1 PlaybackControlsRowPresenter (android.support.v17.leanback.widget.PlaybackControlsRowPresenter)1