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

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

Introduction

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

Prototype

int FLAG_HANDLES_MEDIA_BUTTONS

To view the source code for android.support.v4.media.session MediaSessionCompat FLAG_HANDLES_MEDIA_BUTTONS.

Click Source Link

Document

Sets this flag on the session to indicate that it can handle media button events.

Usage

From source file:com.example.android.leanback.PlaybackTransportControlGlueSample.java

/**
 * Public api to connect media session to this glue
 *///from   w w  w  .j  a  v a  2 s  .co m
public void connectToMediaSession(MediaSessionCompat mediaSessionCompat) {
    mMediaSessionCompat = mediaSessionCompat;
    mMediaSessionCompat.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mMediaSessionCompat.setActive(true);
    mMediaSessionCompat.setCallback(new MediaSessionCallback());
    onMediaSessionMetaDataChanged();
}

From source file:github.popeen.dsub.util.compat.RemoteControlClientLP.java

@Override
public void register(Context context, ComponentName mediaButtonReceiverComponent) {
    downloadService = (DownloadService) context;
    mediaSession = new MediaSessionCompat(downloadService, "DSub MediaSession");

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
    PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0,
            mediaButtonIntent, 0);/*ww w . j a  va  2  s  .  c  o  m*/
    mediaSession.setMediaButtonReceiver(mediaPendingIntent);

    Intent activityIntent = new Intent(context, SubsonicFragmentActivity.class);
    activityIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
    activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
    mediaSession.setSessionActivity(activityPendingIntent);

    mediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setCallback(new EventCallback());

    mediaSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
    mediaSession.setActive(true);

    Bundle sessionExtras = new Bundle();
    sessionExtras.putBoolean(WEAR_BACKGROUND_THEME, true);
    sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_PREVIOUS, true);
    sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_NEXT, true);
    sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_PREVIOUS, true);
    sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_NEXT, true);
    mediaSession.setExtras(sessionExtras);

    imageLoader = SubsonicActivity.getStaticImageLoader(context);
}

From source file:com.example.android.supportv4.media.MediaBrowserServiceSupport.java

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate");

    mPlayingQueue = new ArrayList<>();
    mMusicProvider = new MusicProvider();
    mPackageValidator = new PackageValidator(this);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(new MediaSessionCallback());
    mSession.setFlags(//from  ww  w . j a va2s  .com
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mPlayback = new Playback(this, mMusicProvider);
    mPlayback.setState(PlaybackStateCompat.STATE_NONE);
    mPlayback.setCallback(this);
    mPlayback.start();

    Context context = getApplicationContext();
    Intent intent = new Intent(context, MediaBrowserSupport.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    Bundle extras = new Bundle();
    CarHelper.setSlotReservationFlags(extras, true, true, true);
    mSession.setExtras(extras);

    updatePlaybackState(null);

    mMediaNotificationManager = new MediaNotificationManager(this);
}

From source file:com.example.android.mediabrowserservice.MusicService.java

@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");

    mPlayingQueue = new ArrayList<>();
    mMusicProvider = new MusicProvider();
    mPackageValidator = new PackageValidator(this);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService",
            new ComponentName(this, MediaNotificationManager.class), null);
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(new MediaSessionCallback());
    mSession.setFlags(//  w  w w . j  a v  a 2  s.  co m
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mPlayback = new Playback(this, mMusicProvider);
    mPlayback.setState(PlaybackStateCompat.STATE_NONE);
    mPlayback.setCallback(this);
    mPlayback.start();

    Context context = getApplicationContext();
    Intent intent = new Intent(context, MusicPlayerActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    Bundle extras = new Bundle();
    CarHelper.setSlotReservationFlags(extras, true, true, true);
    mSession.setExtras(extras);

    updatePlaybackState(null);

    mMediaNotificationManager = new MediaNotificationManager(this);
}

From source file:nuclei.media.MediaService.java

@Override
public void onCreate() {
    super.onCreate();
    LOG.d("onCreate");

    mPackageValidator = new PackageValidator(this);

    boolean casting = false;
    try {/*  w w w .  j a v a 2  s  . c o  m*/
        VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer);
        casting = VideoCastManager.getInstance().isConnected() || VideoCastManager.getInstance().isConnecting();
    } catch (IllegalStateException err) {
        LOG.e("Error registering cast consumer : " + err.getMessage());
    }

    Playback playback;

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "NucleiMediaService",
            new ComponentName(getApplicationContext(), MediaButtonReceiver.class), null);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    setSessionToken(mSession.getSessionToken());

    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }

    mSessionExtras = new Bundle();

    if (casting) {
        mSessionExtras.putString(EXTRA_CONNECTED_CAST, VideoCastManager.getInstance().getDeviceName());
        mMediaRouter.setMediaSessionCompat(mSession);
        playback = PlaybackFactory.createCastPlayback(MediaService.this);
    } else
        playback = PlaybackFactory.createLocalPlayback(MediaService.this);

    mPlaybackManager = new PlaybackManager(this, playback);

    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(new ComponentName(this, MediaService.class));
    mSession.setMediaButtonReceiver(PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0));

    CarHelper.setSlotReservationFlags(mSessionExtras, true, true, true);
    mSession.setExtras(mSessionExtras);

    mPlaybackManager.updatePlaybackState(null, true);

    registerCarConnectionReceiver();
}

From source file:com.appdevper.mediaplayer.app.MusicService.java

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate");

    mPlayingQueue = new ArrayList<>();
    mMusicProvider = MusicProvider.getInstance();

    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(),
            new QueueManager.MetadataUpdateListener() {
                @Override/*ww  w .j  a  va  2s  .c o  m*/
                public void onMetadataChanged(MediaMetadataCompat metadata) {
                    mSession.setMetadata(metadata);
                }

                @Override
                public void onMetadataRetrieveError() {
                    mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata));
                }

                @Override
                public void onCurrentQueueIndexUpdated(int queueIndex) {
                    mPlaybackManager.handlePlayRequest();
                }

                @Override
                public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
                    mSession.setQueue(newQueue);
                    mSession.setQueueTitle(title);
                }
            });

    LocalPlayback playback = new LocalPlayback(this, mMusicProvider);

    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    //setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, MainActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    mSessionExtras = new Bundle();

    mSession.setExtras(mSessionExtras);

    mPlaybackManager.updatePlaybackState(null);

    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }
    VideoCastManager.getInstance().addVideoCastConsumer(mCastConsumer);
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());

}

From source file:com.example.android.leanback.MediaSessionService.java

@Override
public void onCreate() {
    super.onCreate();

    // This service can be created for multiple times, the objects will only be created when
    // it is null
    if (mMediaSession == null) {
        mMediaSession = new MediaSessionCompat(this, MUSIC_PLAYER_SESSION_TOKEN);
        mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mMediaSession.setCallback(new MediaSessionCallback());
    }//from   w  ww .  ja  va  2  s  .  co  m

    if (mAudioManager == null) {
        // Create audio manager through system service
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    }

    // initialize the player (including activate media session, request audio focus and
    // set up the listener to listen to player's state)
    initializePlayer();
}

From source file:com.example.lzhang.stockchartt.media.MusicService.java

@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");

    mMusicProvider = new MusicProvider();

    // To make the app more responsive, fetch and cache catalog information now.
    // This can help improve the response time in the method
    // {@link #onLoadChildren(String, Result<List<MediaItem>>) onLoadChildren()}.
    //         mMusicProvider.retrieveMediaAsync(null /* Callback */);

    mPackageValidator = new PackageValidator(this);

    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(),
            new QueueManager.MetadataUpdateListener() {
                @Override/*from   w  ww. ja  v a2s .c  o  m*/
                public void onMetadataChanged(MediaMetadataCompat metadata) {
                    mSession.setMetadata(metadata);
                }

                @Override
                public void onMetadataRetrieveError() {
                    mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata));
                }

                @Override
                public void onCurrentQueueIndexUpdated(int queueIndex) {
                    mPlaybackManager.handlePlayRequest();
                }

                @Override
                public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
                    mSession.setQueue(newQueue);
                    mSession.setQueueTitle(title);
                }
            });

    LocalPlayback playback = new LocalPlayback(this, mMusicProvider);
    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, MusicPlayerActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    mSessionExtras = new Bundle();
    mSession.setExtras(mSessionExtras);

    mPlaybackManager.updatePlaybackState(null);

    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }
}

From source file:org.copticchurchlibrary.arabicreader.MusicService.java

@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");

    mMusicProvider = new MusicProvider();

    // To make the app more responsive, fetch and cache catalog information now.
    // This can help improve the response time in the method
    // {@link #onLoadChildren(String, Result<List<MediaItem>>) onLoadChildren()}.
    mMusicProvider.retrieveMediaAsync(null /* Callback */);

    mPackageValidator = new PackageValidator(this);

    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(),
            new QueueManager.MetadataUpdateListener() {
                @Override//ww w.j a  va2  s. c  om
                public void onMetadataChanged(MediaMetadataCompat metadata) {
                    mSession.setMetadata(metadata);
                }

                @Override
                public void onMetadataRetrieveError() {
                    mPlaybackManager.updatePlaybackState(
                            getString(org.copticchurchlibrary.arabicreader.R.string.error_no_metadata));
                }

                @Override
                public void onCurrentQueueIndexUpdated(int queueIndex) {
                    mPlaybackManager.handlePlayRequest();
                }

                @Override
                public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
                    mSession.setQueue(newQueue);
                    mSession.setQueueTitle(title);
                }
            });

    LocalPlayback playback = new LocalPlayback(this, mMusicProvider);
    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, NowPlayingActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    mSessionExtras = new Bundle();
    CarHelper.setSlotReservationFlags(mSessionExtras, true, true, true);
    WearHelper.setSlotReservationFlags(mSessionExtras, true, true);
    WearHelper.setUseBackgroundFromTheme(mSessionExtras, true);
    mSession.setExtras(mSessionExtras);

    mPlaybackManager.updatePlaybackState(null);

    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }

    if (!TvHelper.isTvUiMode(this)) {
        mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
        mCastSessionManagerListener = new CastSessionManagerListener();
        mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class);
    }

    mMediaRouter = MediaRouter.getInstance(getApplicationContext());

    registerCarConnectionReceiver();
}

From source file:com.murati.oszk.audiobook.MusicService.java

@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");

    mMusicProvider = new MusicProvider(this.getApplicationContext());

    // To make the app more responsive, fetch and cache catalog information now.
    // This can help improve the response time in the method
    // {@link #onLoadChildren(String, Result<List<MediaItem>>) onLoadChildren()}.
    mMusicProvider.retrieveMediaAsync(null /* Callback */);

    mPackageValidator = new PackageValidator(this);

    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(),
            new QueueManager.MetadataUpdateListener() {
                @Override//from   www. j  a va2s  .  co  m
                public void onMetadataChanged(MediaMetadataCompat metadata) {
                    mSession.setMetadata(metadata);
                }

                @Override
                public void onMetadataRetrieveError() {
                    mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata));
                }

                @Override
                public void onCurrentQueueIndexUpdated(int queueIndex) {
                    mPlaybackManager.handlePlayRequest();
                }

                @Override
                public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
                    mSession.setQueue(newQueue);
                    mSession.setQueueTitle(title);
                }
            });

    LocalPlayback playback = new LocalPlayback(this, mMusicProvider);
    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    Context context = getApplicationContext();
    Intent intent = new Intent(context, NowPlayingActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    mSessionExtras = new Bundle();
    CarHelper.setSlotReservationFlags(mSessionExtras, true, true, true);
    WearHelper.setSlotReservationFlags(mSessionExtras, true, true);
    WearHelper.setUseBackgroundFromTheme(mSessionExtras, true);
    mSession.setExtras(mSessionExtras);

    mPlaybackManager.updatePlaybackState(null);

    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }

    if (!TvHelper.isTvUiMode(this)) {
        mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
        mCastSessionManagerListener = new CastSessionManagerListener();
        mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class);
    }

    mMediaRouter = MediaRouter.getInstance(getApplicationContext());

    registerCarConnectionReceiver();
}