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

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

Introduction

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

Prototype

int FLAG_HANDLES_TRANSPORT_CONTROLS

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

Click Source Link

Document

Sets this flag on the session to indicate that it handles transport control commands through its Callback .

Usage

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  w w  w .j a  va2 s.c om*/
            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(//from   w  w  w.j a  v  a2s.  c o 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 {//from ww  w. j ava2 s  . com
        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/*from  w  ww  .  ja v a 2s  .c  om*/
                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:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java

private void initMediaSession() throws RemoteException {
    if (mediaSessionManager != null)
        return; //mediaSessionManager exists

    mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
    // Create a new MediaSession
    mediaSession = new MediaSessionCompat(getApplicationContext(), "AudioPlayer");
    //Get MediaSessions transport controls
    transportControls = mediaSession.getController().getTransportControls();
    //set MediaSession -> ready to receive media commands
    mediaSession.setActive(true);/* w  w w .j ava 2  s . com*/
    //indicate that the MediaSession handles transport control commands
    // through its MediaSessionCompat.Callback.
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    //Set mediaSession's MetaData
    updateMetaData();

    // Attach Callback to receive MediaSession updates
    mediaSession.setCallback(new MediaSessionCompat.Callback() {
        // Implement callbacks
        @Override
        public void onPlay() {
            super.onPlay();
            resumeMedia();
            publishStatus(STATUS_PLAYING);
            buildNotification(PlaybackStatus.PLAYING);
        }

        @Override
        public void onPause() {
            super.onPause();
            pauseMedia();
            publishStatus(STATUS_PAUSED);
            buildNotification(PlaybackStatus.PAUSED);
        }

        @Override
        public void onSkipToNext() {
            super.onSkipToNext();
            skipToNext();
            updateMetaData();
            buildNotification(PlaybackStatus.PLAYING);
        }

        @Override
        public void onSkipToPrevious() {
            super.onSkipToPrevious();
            skipToPrevious();
            updateMetaData();
            buildNotification(PlaybackStatus.PLAYING);
        }

        @Override
        public void onStop() {
            super.onStop();
            publishStatus(STATUS_STOPED);
            removeNotification();
            //Stop the service
            stopSelf();
        }

    });
}

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());
    }// ww w. j  ava  2 s  .  c om

    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   ww w .j a  va  2 s  .c om
                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// w  w w . j a v  a  2s.c  o m
                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 w  ww .j  a v  a 2s  . 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();
}

From source file:com.example.android.uamp.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/*w  w w  .  j  ava2 s . com*/
                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);
    }

    int playServicesAvailable = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);

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

    mMediaRouter = MediaRouter.getInstance(getApplicationContext());

    registerCarConnectionReceiver();
}