Java Code Examples for android.support.v4.media.session.MediaSessionCompat#setSessionActivity()

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#setSessionActivity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MusicPlaybackService.java    From Melophile with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
  super.onCreate();
  playbackManager.setServiceCallback(this);
  playbackManager.setUpdateListener(this);
  mediaSession = new MediaSessionCompat(getApplicationContext(), TAG);
  mediaSession.setCallback(playbackManager.getMediaSessionCallback());
  mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
          MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
  setSessionToken(mediaSession.getSessionToken());
  Context context = getApplicationContext();
  Intent intent = new Intent(context, TrackActivity.class);
  PendingIntent pi = PendingIntent.getActivity(context, 99,
          intent, PendingIntent.FLAG_UPDATE_CURRENT);
  mediaSession.setSessionActivity(pi);
  notification = new TrackNotification(this);
  playbackManager.updatePlaybackState(PlaybackStateCompat.STATE_NONE);
}
 
Example 2
Source File: MusicPlayer.java    From Jockey with Apache License 2.0 6 votes vote down vote up
/**
 * Initiate a MediaSession to allow the Android system to interact with the player
 */
private void initMediaSession() {
    Timber.i("Initializing MediaSession");
    ComponentName mbrComponent = new ComponentName(mContext, MediaButtonReceiver.class.getName());
    mMediaSession = new MediaSessionCompat(mContext, TAG, mbrComponent, null);
    mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
            | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mMediaSession.setCallback(new MediaSessionCallback(this, mMediaBrowserRoot));
    mMediaSession.setSessionActivity(
            PendingIntent.getActivity(
                    mContext, 0,
                    LibraryActivity.newNowPlayingIntent(mContext)
                            .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_CANCEL_CURRENT));

    updateMediaSession();

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setClass(mContext, MediaButtonReceiver.class);
    PendingIntent mbrIntent = PendingIntent.getBroadcast(mContext, 0, mediaButtonIntent, 0);
    mMediaSession.setMediaButtonReceiver(mbrIntent);

    mMediaSession.setActive(true);
}
 
Example 3
Source File: MediaSessionFactoryImpl.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
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;
}
 
Example 4
Source File: MusicService.java    From klingar with Apache License 2.0 5 votes vote down vote up
@Override public void onCreate() {
  super.onCreate();
  Timber.d("onCreate");
  KlingarApp.get(this).component().inject(this);

  Playback playback = new LocalPlayback(getApplicationContext(), musicController, audioManager,
      wifiManager, client);
  playbackManager = new PlaybackManager(queueManager, this, AndroidClock.DEFAULT, playback);

  session = new MediaSessionCompat(this, "MusicService");

  try {
    MediaControllerCompat mediaController =
        new MediaControllerCompat(this.getApplicationContext(), session.getSessionToken());
    musicController.setMediaController(mediaController);
  } catch (RemoteException e) {
    Timber.e(e, "Could not create MediaController");
    throw new IllegalStateException();
  }

  session.setCallback(playbackManager.getMediaSessionCallback());

  Context context = getApplicationContext();
  Intent intent = new Intent(context, KlingarActivity.class);
  session.setSessionActivity(PendingIntent.getActivity(context, 99, intent, FLAG_UPDATE_CURRENT));

  playbackManager.updatePlaybackState();

  mediaNotificationManager = new MediaNotificationManager(this, musicController,
      queueManager, rx);

  castSessionManager = CastContext.getSharedInstance(this).getSessionManager();
  castSessionManagerListener = new CastSessionManagerListener();
  castSessionManager.addSessionManagerListener(castSessionManagerListener, CastSession.class);

  mediaRouter = MediaRouter.getInstance(getApplicationContext());

  timelineManager = new TimelineManager(musicController, queueManager, media, rx);
  timelineManager.start();
}
 
Example 5
Source File: RemoteControlClientLP.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
@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);
	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);
}
 
Example 6
Source File: PlayerService.java    From AndroidAudioExample with MIT License 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
        audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                .setOnAudioFocusChangeListener(audioFocusChangeListener)
                .setAcceptsDelayedFocusGain(false)
                .setWillPauseWhenDucked(true)
                .setAudioAttributes(audioAttributes)
                .build();
    }

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    mediaSession = new MediaSessionCompat(this, "PlayerService");
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(mediaSessionCallback);

    Context appContext = getApplicationContext();

    Intent activityIntent = new Intent(appContext, MainActivity.class);
    mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0));

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class);
    mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0));

    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
    exoPlayer.addListener(exoPlayerListener);
    DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name)));
    Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max
    this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
    this.extractorsFactory = new DefaultExtractorsFactory();
}
 
Example 7
Source File: MusicService.java    From LyricHere with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    LogUtils.d(TAG, "onCreate");

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

    mEventReceiver = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mEventReceiver);
    mMediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService", mEventReceiver, mMediaPendingIntent);

    final MediaSessionCallback cb = new MediaSessionCallback();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Shouldn't really have to do this but the MediaSessionCompat method uses
        // an internal proxy class, which doesn't forward events such as
        // onPlayFromMediaId when running on Lollipop.
        final MediaSession session = (MediaSession) mSession.getMediaSession();
        session.setCallback(new MediaSessionCallbackProxy(cb));
    } else {
        mSession.setCallback(cb);
    }

    setSessionToken(mSession.getSessionToken());
    mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mPlayback = new LocalPlayback(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);

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

    updatePlaybackState(null);

    mMediaNotificationManager = new MediaNotificationManager(this);
}