Java Code Examples for androidx.media.session.MediaButtonReceiver#handleIntent()

The following examples show how to use androidx.media.session.MediaButtonReceiver#handleIntent() . 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: ServicePlayMusic.java    From Bop with Apache License 2.0 6 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    if (intent != null) {
        String action = intent.getAction();
        String command = intent.getStringExtra(CMD_NAME);
        if (ACTION_CMD.equals(action)) {
            if (ACTION_PAUSE.equals(command)) {
                pausePlayer();
            }
        } else {
            MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent);
        }
    }
    mDelayedStopHandler.removeCallbacksAndMessages(null);
    mDelayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY);

    return START_STICKY;
}
 
Example 2
Source File: VinylCastService.java    From vinyl-cast with MIT License 6 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Timber.d("onStartCommand");

    if (castSessionManagerListener == null) {
        castSessionManagerListener = new CastSessionManagerListener();
        ((VinylCastApplication)getApplication()).getCastSessionManager().addSessionManagerListener(castSessionManagerListener);
    }

    String action = intent.getAction();
    Timber.d("onStartCommand received action: " + action);

    switch (action) {
        case VinylCastService.ACTION_START_RECORDING:
            engage();
            break;
        case VinylCastService.ACTION_STOP_RECORDING:
            disengage(false);
            break;
        default:
            MediaButtonReceiver.handleIntent(mediaSession, intent);
            break;
    }

    return START_NOT_STICKY;
}
 
Example 3
Source File: PlayerService.java    From Jockey with Apache License 2.0 6 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Timber.i("onStartCommand called");
    super.onStartCommand(intent, flags, startId);

    if (intent != null && MediaStoreUtil.hasPermission(this)) {
        if (mBeQuiet) {
            mBeQuiet = intent.getBooleanExtra(EXTRA_START_SILENT, true);
        }

        if (intent.hasExtra(Intent.EXTRA_KEY_EVENT)) {
            notifyNowPlaying(true);
            MediaButtonReceiver.handleIntent(musicPlayer.getMediaSession(), intent);
            Timber.i(intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT).toString());
        }
    }
    return START_STICKY;
}
 
Example 4
Source File: MediaPlaybackService.java    From react-native-jw-media-player with MIT License 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
	if (mMediaSessionManager != null) {
		MediaButtonReceiver.handleIntent(mMediaSessionManager.getMediaSession(), intent);
	}
	return START_STICKY;
}
 
Example 5
Source File: MusicService.java    From klingar with Apache License 2.0 5 votes vote down vote up
@Override public int onStartCommand(Intent startIntent, int flags, int startId) {
  if (startIntent != null) {
    if (ACTION_STOP_CASTING.equals(startIntent.getAction())) {
      CastContext.getSharedInstance(this).getSessionManager().endCurrentSession(true);
    } else {
      // Try to handle the intent as a media button event wrapped by MediaButtonReceiver
      MediaButtonReceiver.handleIntent(session, startIntent);
    }
  }
  // Reset the delay handler to enqueue a message to stop the service if nothing is playing
  delayedStopHandler.removeCallbacksAndMessages(null);
  delayedStopHandler.sendEmptyMessageDelayed(0, STOP_DELAY);
  return START_STICKY;
}
 
Example 6
Source File: TtsService.java    From android-app with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "onStartCommand() " + intent);

    if (intent != null) {
        if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
            // `MediaButtonReceiver` may use `Context.startForegroundService(Intent)`,
            // so we *have* to call `startForeground(...)`
            // or the app will crash during service shutdown

            setForegroundAndNotification(true);
        }

        MediaButtonReceiver.handleIntent(mediaSession, intent);

        String action = intent.getAction();
        if (ACTION_PLAY.equals(action)) {
            playCmd();
        } else if (ACTION_PAUSE.equals(action)) {
            pauseCmd();
        } else if (ACTION_PLAY_PAUSE.equals(action)) {
            playPauseCmd();
        } else if (ACTION_REWIND.equals(action)) {
            rewindCmd();
        } else if (ACTION_FAST_FORWARD.equals(action)) {
            fastForwardCmd();
        }
    }

    return super.onStartCommand(intent, flags, startId);
}
 
Example 7
Source File: NotificationPanel.java    From flutter_media_notification with MIT License 4 votes vote down vote up
@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        boolean isPlaying = intent.getBooleanExtra("isPlaying", true);
        String title = intent.getStringExtra("title");
        String author = intent.getStringExtra("author");

        createNotificationChannel();


        MediaSessionCompat mediaSession = new MediaSessionCompat(this, MEDIA_SESSION_TAG);


        int iconPlayPause = R.drawable.baseline_play_arrow_black_48;
        String titlePlayPause = "pause";
        if(isPlaying){
            iconPlayPause=R.drawable.baseline_pause_black_48;
            titlePlayPause="play";
        }

        Intent toggleIntent = new Intent(this, NotificationReturnSlot.class)
                .setAction("toggle")
                .putExtra("title",  title)
                .putExtra("author",  author)
                .putExtra("play", !isPlaying);
        PendingIntent pendingToggleIntent = PendingIntent.getBroadcast(this, 0, toggleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        MediaButtonReceiver.handleIntent(mediaSession, toggleIntent);

        //TODO(ALI): add media mediaSession Buttons and handle them
        Intent nextIntent = new Intent(this, NotificationReturnSlot.class)
                .setAction("next");
        PendingIntent pendingNextIntent = PendingIntent.getBroadcast(this, 0, nextIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//        MediaButtonReceiver.handleIntent(mediaSession, nextIntent);

        Intent prevIntent = new Intent(this, NotificationReturnSlot.class)
                .setAction("prev");
        PendingIntent pendingPrevIntent = PendingIntent.getBroadcast(this, 0, prevIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//        MediaButtonReceiver.handleIntent(mediaSession, prevIntent);

        Intent selectIntent = new Intent(this, NotificationReturnSlot.class)
                .setAction("select");
        PendingIntent selectPendingIntent = PendingIntent.getBroadcast(this, 0, selectIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//        MediaButtonReceiver.handleIntent(mediaSession, selectIntent);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .addAction(R.drawable.baseline_skip_previous_black_48, "prev", pendingPrevIntent)
                .addAction(iconPlayPause, titlePlayPause, pendingToggleIntent)
                .addAction(R.drawable.baseline_skip_next_black_48, "next", pendingNextIntent)
                .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                        .setShowActionsInCompactView(0, 1,2)
                        .setShowCancelButton(true)
                        .setMediaSession(mediaSession.getSessionToken()))
                .setSmallIcon(R.drawable.ic_stat_music_note)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                .setVibrate(new long[]{0L})
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentTitle(title)
                .setContentText(author)
                .setSubText(title)
                .setContentIntent(selectPendingIntent)
                .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_stat_music_note))
                .build();

        startForeground(NOTIFICATION_ID, notification);
        if(!isPlaying) {
            stopForeground(false);
        }

        return START_NOT_STICKY;
    }
 
Example 8
Source File: PlayerService.java    From AndroidAudioExample with MIT License 4 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MediaButtonReceiver.handleIntent(mediaSession, intent);
    return super.onStartCommand(intent, flags, startId);
}