Example usage for android.support.v4.media.session MediaSessionCompat setFlags

List of usage examples for android.support.v4.media.session MediaSessionCompat setFlags

Introduction

In this page you can find the example usage for android.support.v4.media.session MediaSessionCompat setFlags.

Prototype

public void setFlags(@SessionFlags int flags) 

Source Link

Document

Sets any flags for the session.

Usage

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MediaSessionCompat mediaSession = new MediaSessionCompat(this, getApplication().getPackageName());
    mediaSession.setCallback(mMediaSessionCallback);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setActive(true);/*from  w w  w .  j  a  v  a  2s .c o m*/
    PlaybackStateCompat state = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY
            | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PAUSE
            | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS).build();
    mediaSession.setPlaybackState(state);

}

From source file:com.doctoror.fuckoffmusicplayer.data.media.session.MediaSessionFactoryImpl.java

@NonNull
@Override//w  w  w.  j av a  2  s  . co  m
public MediaSessionCompat newMediaSession() {
    final MediaSessionCompat mediaSession = new MediaSessionCompat(context, TAG_MEDIA_SESSION,
            mediaButtonReceiver, mediaButtonIntent);
    mediaSession.setCallback(mediaSessionCallback);
    mediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setSessionActivity(PendingIntent.getActivity(context, 1,
            new Intent(context, sessionActivityClass), PendingIntent.FLAG_UPDATE_CURRENT));
    mediaSession.setActive(true);
    return mediaSession;
}

From source file:android.support.mediacompat.service.ServiceBroadcastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();/*from  ww w  .  ja  v  a  2 s  .co  m*/
    if (ACTION_CALL_MEDIA_BROWSER_SERVICE_METHOD.equals(intent.getAction()) && extras != null) {
        StubMediaBrowserServiceCompat service = StubMediaBrowserServiceCompat.sInstance;
        int method = extras.getInt(KEY_METHOD_ID, 0);

        switch (method) {
        case NOTIFY_CHILDREN_CHANGED:
            service.notifyChildrenChanged(extras.getString(KEY_ARGUMENT));
            break;
        case SEND_DELAYED_NOTIFY_CHILDREN_CHANGED:
            service.sendDelayedNotifyChildrenChanged();
            break;
        case SEND_DELAYED_ITEM_LOADED:
            service.sendDelayedItemLoaded();
            break;
        case CUSTOM_ACTION_SEND_PROGRESS_UPDATE:
            service.mCustomActionResult.sendProgressUpdate(extras.getBundle(KEY_ARGUMENT));
            break;
        case CUSTOM_ACTION_SEND_ERROR:
            service.mCustomActionResult.sendError(extras.getBundle(KEY_ARGUMENT));
            break;
        case CUSTOM_ACTION_SEND_RESULT:
            service.mCustomActionResult.sendResult(extras.getBundle(KEY_ARGUMENT));
            break;
        case SET_SESSION_TOKEN:
            StubMediaBrowserServiceCompatWithDelayedMediaSession.sInstance.callSetSessionToken();
            break;
        }
    } else if (ACTION_CALL_MEDIA_SESSION_METHOD.equals(intent.getAction()) && extras != null) {
        MediaSessionCompat session = StubMediaBrowserServiceCompat.sSession;
        int method = extras.getInt(KEY_METHOD_ID, 0);

        switch (method) {
        case SET_EXTRAS:
            session.setExtras(extras.getBundle(KEY_ARGUMENT));
            break;
        case SET_FLAGS:
            session.setFlags(extras.getInt(KEY_ARGUMENT));
            break;
        case SET_METADATA:
            session.setMetadata((MediaMetadataCompat) extras.getParcelable(KEY_ARGUMENT));
            break;
        case SET_PLAYBACK_STATE:
            session.setPlaybackState((PlaybackStateCompat) extras.getParcelable(KEY_ARGUMENT));
            break;
        case SET_QUEUE:
            List<QueueItem> items = extras.getParcelableArrayList(KEY_ARGUMENT);
            session.setQueue(items);
            break;
        case SET_QUEUE_TITLE:
            session.setQueueTitle(extras.getCharSequence(KEY_ARGUMENT));
            break;
        case SET_SESSION_ACTIVITY:
            session.setSessionActivity((PendingIntent) extras.getParcelable(KEY_ARGUMENT));
            break;
        case SET_CAPTIONING_ENABLED:
            session.setCaptioningEnabled(extras.getBoolean(KEY_ARGUMENT));
            break;
        case SET_REPEAT_MODE:
            session.setRepeatMode(extras.getInt(KEY_ARGUMENT));
            break;
        case SET_SHUFFLE_MODE:
            session.setShuffleMode(extras.getInt(KEY_ARGUMENT));
            break;
        case SEND_SESSION_EVENT:
            Bundle arguments = extras.getBundle(KEY_ARGUMENT);
            session.sendSessionEvent(arguments.getString("event"), arguments.getBundle("extras"));
            break;
        case SET_ACTIVE:
            session.setActive(extras.getBoolean(KEY_ARGUMENT));
            break;
        case RELEASE:
            session.release();
            break;
        case SET_PLAYBACK_TO_LOCAL:
            session.setPlaybackToLocal(extras.getInt(KEY_ARGUMENT));
            break;
        case SET_PLAYBACK_TO_REMOTE:
            ParcelableVolumeInfo volumeInfo = extras.getParcelable(KEY_ARGUMENT);
            session.setPlaybackToRemote(new VolumeProviderCompat(volumeInfo.controlType, volumeInfo.maxVolume,
                    volumeInfo.currentVolume) {
            });
            break;
        case SET_RATING_TYPE:
            session.setRatingType(RatingCompat.RATING_5_STARS);
            break;
        }
    }
}

From source file:org.chromium.chrome.browser.media.ui.MediaNotificationManager.java

private MediaSessionCompat createMediaSession() {
    MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, mContext.getString(R.string.app_name),
            new ComponentName(mContext.getPackageName(), getButtonReceiverClassName()), null);
    mediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(mMediaSessionCallback);

    // TODO(mlamouri): the following code is to work around a bug that hopefully
    // MediaSessionCompat will handle directly. see b/24051980.
    try {/*from w ww. java  2s.co m*/
        mediaSession.setActive(true);
    } catch (NullPointerException e) {
        // Some versions of KitKat do not support AudioManager.registerMediaButtonIntent
        // with a PendingIntent. They will throw a NullPointerException, in which case
        // they should be able to activate a MediaSessionCompat with only transport
        // controls.
        mediaSession.setActive(false);
        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mediaSession.setActive(true);
    }
    return mediaSession;
}

From source file:com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector.java

/**
 * Creates an instance. Must be called on the same thread that is used to construct the player
 * instances passed to {@link #setPlayer(Player, PlaybackPreparer, CustomActionProvider...)}.
 *
 * @param mediaSession The {@link MediaSessionCompat} to connect to.
 * @param playbackController A {@link PlaybackController} for handling playback actions, or
 *     {@code null} if the connector should handle playback actions directly.
 * @param doMaintainMetadata Whether the connector should maintain the metadata of the session. If
 *     {@code false}, you need to maintain the metadata of the media session yourself (provide at
 *     least the duration to allow clients to show a progress bar).
 *///from ww  w.  j  ava2 s .  c om
public MediaSessionConnector(MediaSessionCompat mediaSession, PlaybackController playbackController,
        boolean doMaintainMetadata) {
    this.mediaSession = mediaSession;
    this.playbackController = playbackController != null ? playbackController : new DefaultPlaybackController();
    this.handler = new Handler(Looper.myLooper() != null ? Looper.myLooper() : Looper.getMainLooper());
    this.doMaintainMetadata = doMaintainMetadata;
    mediaSession.setFlags(BASE_MEDIA_SESSION_FLAGS);
    mediaController = mediaSession.getController();
    mediaSessionCallback = new MediaSessionCallback();
    exoPlayerEventListener = new ExoPlayerEventListener();
    customActionMap = Collections.emptyMap();
    commandMap = new HashMap<>();
    registerCommandReceiver(playbackController);
}