android.support.v4.media.session.MediaButtonReceiver Java Examples

The following examples show how to use android.support.v4.media.session.MediaButtonReceiver. 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: AudioPlayerService.java    From react-native-audio-streaming-player with MIT License 6 votes vote down vote up
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
    if (startIntent != null) {
        String action = startIntent.getAction();
        String command = startIntent.getStringExtra(CMD_NAME);
        if (ACTION_CMD.equals(action)) {
            if (CMD_PAUSE.equals(command)) {
                mMediaController.getTransportControls().pause();
            }
        } else {
            // Try to handle the intent as a media button event wrapped by MediaButtonReceiver
            MediaButtonReceiver.handleIntent(mMediaSession, startIntent);
        }
    }

    return START_STICKY;
}
 
Example #2
Source File: PlayerNotification.java    From blade-player with GNU General Public License v3.0 6 votes vote down vote up
PlayerNotification(PlayerService service)
{
    this.mService = service;

    mNotificationManager = NotificationManagerCompat.from(service);

    mPlayAction = new NotificationCompat.Action(R.drawable.play_arrow, mService.getString(R.string.play),
            MediaButtonReceiver.buildMediaButtonPendingIntent(mService, PlaybackStateCompat.ACTION_PLAY));
    mPauseAction = new NotificationCompat.Action(R.drawable.pause_notif, mService.getString(R.string.pause),
            MediaButtonReceiver.buildMediaButtonPendingIntent(mService, PlaybackStateCompat.ACTION_PAUSE));
    mNextAction = new NotificationCompat.Action(R.drawable.next_arrow_notif, mService.getString(R.string.next),
            MediaButtonReceiver.buildMediaButtonPendingIntent(mService, PlaybackStateCompat.ACTION_SKIP_TO_NEXT));
    mPrevAction = new NotificationCompat.Action(R.drawable.prev_arrow_notif, mService.getString(R.string.prev),
            MediaButtonReceiver.buildMediaButtonPendingIntent(mService, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS));

    mNotificationManager.cancelAll();
}
 
Example #3
Source File: BackgroundAudioService.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
void showPausedNotification() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel();
    }
    NotificationCompat.Builder builder = MediaStyleHelper.from(this, mMediaSessionCompat);
    if( builder == null ) {
        return;
    }

    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_previous, "Prev", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_play, "Play", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_next, "Next", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));


    builder.setStyle(new MediaStyle().setShowActionsInCompactView(0, 1, 2).setMediaSession(mMediaSessionCompat.getSessionToken()));
    builder.setSmallIcon(android.R.drawable.stat_notify_sync);
    Notification notification = builder.build();

    NotificationManagerCompat.from(this).notify(1, notification);
}
 
Example #4
Source File: BackgroundAudioService.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
void showPlayingNotification() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannel();
    }
    NotificationCompat.Builder builder = MediaStyleHelper.from(BackgroundAudioService.this, mMediaSessionCompat);
    if( builder == null ) {
        return;
    }

    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_previous, "Prev", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)));
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_pause, "Pause", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_PLAY_PAUSE)));
    builder.addAction(new NotificationCompat.Action(android.R.drawable.ic_media_next, "Next", MediaButtonReceiver.buildMediaButtonPendingIntent(this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));



    builder.setStyle(new MediaStyle()
            .setShowActionsInCompactView(0, 1, 2)
            .setShowCancelButton(true)
            .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
                    this, PlaybackStateCompat.ACTION_STOP))
            .setMediaSession(mMediaSessionCompat.getSessionToken()));
    builder.setSmallIcon(android.R.drawable.stat_notify_sync);
    Notification notification = builder.build();

    NotificationManagerCompat.from(BackgroundAudioService.this).notify(1, notification);
}
 
Example #5
Source File: AudioPlayerService.java    From react-native-streaming-audio-player with MIT License 6 votes vote down vote up
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
    if (startIntent != null) {
        String action = startIntent.getAction();
        String command = startIntent.getStringExtra(CMD_NAME);
        if (ACTION_CMD.equals(action)) {
            if (CMD_PAUSE.equals(command)) {
                mMediaController.getTransportControls().pause();
            }
        } else {
            // Try to handle the intent as a media button event wrapped by MediaButtonReceiver
            MediaButtonReceiver.handleIntent(mMediaSession, startIntent);
        }
    }

    return START_STICKY;
}
 
Example #6
Source File: NotificationUtil.java    From Prodigal with Apache License 2.0 6 votes vote down vote up
private NotificationCompat.Builder notificationBuilder(PlayerService context, MediaSessionCompat session) {
    MediaControllerCompat controller = session.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat description = mediaMetadata.getDescription();

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSubText(description.getDescription())
            .setLargeIcon(mediaMetadata.getBitmap(MediaMetadataCompat.METADATA_KEY_ART))
            .setContentIntent(clickIntent)
            .setDeleteIntent(
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context, receiverName, PlaybackStateCompat.ACTION_STOP))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
 
Example #7
Source File: MediaSessionFactoryImpl.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
public MediaSessionFactoryImpl(
        @NonNull final Context context,
        @NonNull final Class<? extends Activity> sessionActivityClass,
        @NonNull final MediaSessionCompat.Callback mediaSessionCallback) {
    this.context = context;
    this.sessionActivityClass = sessionActivityClass;
    this.mediaSessionCallback = mediaSessionCallback;

    this.mediaButtonReceiver = new ComponentName(context, MediaButtonReceiver.class);
    this.mediaButtonIntent = PendingIntent.getBroadcast(
            context,
            1,
            new Intent(context, MediaButtonReceiver.class),
            PendingIntent.FLAG_UPDATE_CURRENT);
}
 
Example #8
Source File: MediaNotificationManager.java    From android-MediaBrowserService with Apache License 2.0 5 votes vote down vote up
public MediaNotificationManager(MusicService service) {
    mService = service;

    mNotificationManager =
            (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);

    mPlayAction =
            new NotificationCompat.Action(
                    R.drawable.ic_play_arrow_white_24dp,
                    mService.getString(R.string.label_play),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                            mService,
                            PlaybackStateCompat.ACTION_PLAY));
    mPauseAction =
            new NotificationCompat.Action(
                    R.drawable.ic_pause_white_24dp,
                    mService.getString(R.string.label_pause),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                            mService,
                            PlaybackStateCompat.ACTION_PAUSE));
    mNextAction =
            new NotificationCompat.Action(
                    R.drawable.ic_skip_next_white_24dp,
                    mService.getString(R.string.label_next),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                            mService,
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT));
    mPrevAction =
            new NotificationCompat.Action(
                    R.drawable.ic_skip_previous_white_24dp,
                    mService.getString(R.string.label_previous),
                    MediaButtonReceiver.buildMediaButtonPendingIntent(
                            mService,
                            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS));

    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}
 
Example #9
Source File: BackgroundAudioService.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private void initMediaSession() {
    ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
    mMediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "Tag", mediaButtonReceiver, null);

    mMediaSessionCompat.setCallback(mMediaSessionCallback);
    mMediaSessionCompat.setFlags( MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS );

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
    mMediaSessionCompat.setMediaButtonReceiver(pendingIntent);

    setSessionToken(mMediaSessionCompat.getSessionToken());
}
 
Example #10
Source File: MediaStyleHelper.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Build a notification using the information from the given media session. Makes heavy use
 * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
 * @param context Context used to construct the notification.
 * @param mediaSession Media session to get information.
 * @return A pre-built notification with information from the given media session.
 */
public static NotificationCompat.Builder from(
        Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    if (controller == null) {
        return null;
    }
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    if (mediaMetadata == null) {
        return null;

    }

    MediaDescriptionCompat description = mediaMetadata.getDescription();
    if (description == null) {
        return null;
    }

 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, BackgroundAudioService.CHANNEL_ID);
    builder
            .setContentTitle(description.getTitle())
            .setContentText(description.getSubtitle())
            .setSubText(description.getDescription())
            .setLargeIcon(description.getIconBitmap())
            .setContentIntent(controller.getSessionActivity())

            .setDeleteIntent(
                    MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP))
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
 
Example #11
Source File: PlayerService.java    From Prodigal with Apache License 2.0 5 votes vote down vote up
private void initMediaSession() {
        ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
        mediaSession = new MediaSessionCompat(getApplicationContext(), "PrdPlayer", mediaButtonReceiver, null);
//        mediaSession.setCallback(mMediaSessionCallback);
        mediaSession.setFlags( MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS );

        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
        mediaSession.setMediaButtonReceiver(pendingIntent);
        setSessionToken(mediaSession.getSessionToken());

    }
 
Example #12
Source File: NotificationUtil.java    From Prodigal with Apache License 2.0 5 votes vote down vote up
private NotificationUtil(Context context){
    appContext = context;
    notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
    init();

    builder = new NotificationCompat.Builder(appContext);
    builder.setCustomBigContentView(bigView)
            .setCustomContentView(normalView)
            .setSmallIcon(R.drawable.pod_notification)
            .setContentIntent(clickIntent);
    notification = builder.build();
    notification.flags = Notification.FLAG_ONGOING_EVENT;
    receiverName = new ComponentName(context.getApplicationContext(), MediaButtonReceiver.class);
}
 
Example #13
Source File: RadioPlayerService.java    From monkeyboard-radio-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.v(TAG, "Got an intent");
    if (intent != null && intent.getAction() != null) {
        Log.v(TAG, "Received intent:" + intent.getAction());

        switch (intent.getAction()) {
            case ACTION_STOP:
                notifyDismissed();
                closeConnection();
                break;
            case ACTION_SEARCH_FORWARDS:
                handleSearchForwards();
                break;
            case ACTION_NEXT:
                handleNextChannelRequest();
                break;
            case ACTION_PAUSE:
                handlePauseRequest();
                break;
            case ACTION_PREVIOUS:
                handlePreviousChannelRequest();
                break;
            case ACTION_SEARCH_BACKWARDS:
                handleSearchBackwards();
                break;
            case ACTION_PLAY:
                handlePlayRequest();
                break;
            default:
                MediaButtonReceiver.handleIntent(mediaSession, intent);
        }
    }
    return START_NOT_STICKY;
}
 
Example #14
Source File: MusicPlaybackService.java    From Melophile with Apache License 2.0 5 votes vote down vote up
@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
  if (startIntent != null) {
    String action = startIntent.getAction();
    if (action != null) {
      if (action.equals(MediaTasks.ACTION_STOP)) {
        stopSelf();
      } else {
        MediaTasks.executeTask(playbackManager, action);
      }
    }
    MediaButtonReceiver.handleIntent(mediaSession, startIntent);
  }
  return START_NOT_STICKY;
}
 
Example #15
Source File: PlayerNotification.java    From blade-player with GNU General Public License v3.0 5 votes vote down vote up
private NotificationCompat.Builder buildNotification(int playerState, MediaSessionCompat.Token token, Song playing)
{
    if(Build.VERSION.SDK_INT >= 26) createChannel();

    boolean isPlaying = (playerState == PlayerMediaPlayer.PLAYER_STATE_PLAYING);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mService, CHANNEL_ID);

    Intent openUI = new Intent(mService, PlayActivity.class);
    openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(mService, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT);

    MediaStyle style = new MediaStyle()
            .setMediaSession(token)
            .setShowActionsInCompactView(0, 1, 2)
            .setShowCancelButton(true)
            .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(mService, PlaybackStateCompat.ACTION_STOP));

    builder.setStyle(style)
            .setWhen(0) //remove 'now' text on notification top
            //.setSubText("") //text between 'Blade' and 'Now' (notification top)
            .setColor(ContextCompat.getColor(mService, ThemesActivity.currentColorPrimary))
            .setSmallIcon(R.drawable.app_icon_notif) //icon that will be displayed in status bar
            .setContentIntent(contentIntent) //intent that will be sent on notification click
            .setLargeIcon(mService.getCurrentArt() == null ? BitmapFactory.decodeResource(mService.getResources(), R.drawable.ic_albums) : mService.getCurrentArt())
            .setContentTitle(playing.getTitle())
            .setContentText(playing.getArtist().getName() + " - " + playing.getAlbum().getName())
            .setDeleteIntent(null) //intent on notification slide
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setOngoing(isPlaying); //disable swipe delete if playing

    builder.addAction(mPrevAction);
    builder.addAction(isPlaying ? mPauseAction : mPlayAction);
    builder.addAction(mNextAction);

    return builder;
}
 
Example #16
Source File: MyMediaBrowserService.java    From carstream-android-auto with Apache License 2.0 4 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MediaButtonReceiver.handleIntent(mediaSessionCompat, intent);
    return super.onStartCommand(intent, flags, startId);
}
 
Example #17
Source File: PlayService.java    From music_player with Open Software License 3.0 4 votes vote down vote up
private void buildNotification(String playState) {
        int notificationAction = R.drawable.ic_pause_black_24dp;//needs to be initialized
        PendingIntent play_pauseIntent = pendingIntent(MyConstant.pauseAction);
        if (playState == MyConstant.pausing) {
            notificationAction = R.drawable.ic_play_arrow_black_24dp;
            play_pauseIntent = pendingIntent(MyConstant.resumeAction);
        }
        Intent startMain = new Intent(PlayService.this, MainActivity.class);
        PendingIntent startMainActivity = PendingIntent.getActivity(PlayService.this, 0, startMain, PendingIntent.FLAG_UPDATE_CURRENT);
        Log.e("服务", "Ongoing" + "MyApplication.getState()" + MyApplication.getState());
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String CHANNEL_ID = "MPlayer";
            CharSequence name = "MPlayer";
            int importance = NotificationManager.IMPORTANCE_LOW;
            NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
            mChannel.enableLights(false);
            mChannel.enableVibration(false);
//            mChannel.setShowBadge(false);
            mNotificationManager.createNotificationChannel(mChannel);

            notification = new Notification.Builder(PlayService.this, CHANNEL_ID)
                    // Show controls on lock screen even when user hides sensitive content.
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setLargeIcon(albumNow)
                    .setSmallIcon(R.drawable.ic_album_black_24dp)
                    .setDeleteIntent(pendingIntent(MyConstant.deleteAction))
//                    .setColor(ColorUtil.getColor(albumNow))
                    .setContentIntent(startMainActivity)
                    .setOngoing(MyApplication.getState().equals(MyConstant.playing))//该选项会导致手表通知不显示
                    .addAction(new Notification.Action(R.drawable.ic_pause_black_24dp, getString(R.string.pause), MediaButtonReceiver.buildMediaButtonPendingIntent(PlayService.this,PlaybackState.ACTION_PLAY_PAUSE)))
                    // Add media control buttons that invoke intents in your media service
//                    .addAction(R.drawable.ic_skip_previous_black_24dp, "SkiptoPrevious", pendingIntent(MyConstant.previousAction)) // #0
//                    .addAction(notificationAction, "Play or Pause", play_pauseIntent)  // #1
//                    .addAction(R.drawable.ic_skip_next_black_24dp, "SkiptoNext", pendingIntent(MyConstant.nextAction))     // #2
                    // Apply the media style template
                    .setStyle(new Notification.MediaStyle()
//                            .setShowActionsInCompactView(0, 1, 2)
                            .setMediaSession(mediaSession.getSessionToken())
                    )
                    .setContentTitle(titleNow)
                    .setContentText(singerNow)
                    .build();
            PlayService.this.startForeground(NOTIFICATION_ID, notification);

//            mNotificationManager.notify(NOTIFICATION_ID, notification);
        } else {
            notification = new Notification.Builder(PlayService.this)
                    // Show controls on lock screen even when user hides sensitive content.
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
                    .setLargeIcon(albumNow)
                    .setSmallIcon(R.drawable.ic_album_black_24dp)
                    .setDeleteIntent(pendingIntent(MyConstant.deleteAction))
//                    .setColor(ColorUtil.getColor(albumNow))
                    .setContentIntent(startMainActivity)
                    .setOngoing(MyApplication.getState().equals(MyConstant.playing))//该选项会导致手表通知不显示
                    // Add media control buttons that invoke intents in your media service
                    .addAction(R.drawable.ic_skip_previous_black_24dp, "SkiptoPrevious", pendingIntent(MyConstant.previousAction)) // #0
                    .addAction(notificationAction, "Play or Pause", play_pauseIntent)  // #1
                    .addAction(R.drawable.ic_skip_next_black_24dp, "SkiptoNext", pendingIntent(MyConstant.nextAction))     // #2
                    // Apply the media style template
                    .setStyle(new Notification.MediaStyle()
                            .setShowActionsInCompactView(0, 1, 2)
                            .setMediaSession(mediaSession.getSessionToken())
                    )
                    .setContentTitle(titleNow)
                    .setContentText(singerNow)
                    .build();
            PlayService.this.startForeground(NOTIFICATION_ID, notification);

//            mNotificationManager.notify(NOTIFICATION_ID, notification);
        }
    }
 
Example #18
Source File: MediaPlaybackService.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 4 votes vote down vote up
public Notification buildMediaNotification() {
  MediaControllerCompat controller = mMediaSession.getController();
  MediaMetadataCompat mediaMetadata = controller.getMetadata();
  MediaDescriptionCompat description = mediaMetadata.getDescription();

  NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

  // Add description metadata from the media session
  builder
    .setContentTitle(description.getTitle())
    .setContentText(description.getSubtitle())
    .setSubText(description.getDescription())
    .setLargeIcon(description.getIconBitmap())
    .setContentIntent(controller.getSessionActivity())
    .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
      this, // Context
      PlaybackStateCompat.ACTION_STOP))
    .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

  // Add branding from your app
  builder
    .setSmallIcon(R.drawable.notification_icon)
    .setColor(ContextCompat.getColor(this, R.color.colorPrimary));

  // Add actions
  builder
    .addAction(new NotificationCompat.Action(
      R.drawable.pause, getString(R.string.pause),
      MediaButtonReceiver.buildMediaButtonPendingIntent(
        this, PlaybackStateCompat.ACTION_PLAY_PAUSE)))
    .addAction(new NotificationCompat.Action(
      R.drawable.skip_to_next, getString(R.string.skip_to_next),
      MediaButtonReceiver.buildMediaButtonPendingIntent(
        this, PlaybackStateCompat.ACTION_SKIP_TO_NEXT)));

  // Add the MediaStyle
  builder
    .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
    .setShowActionsInCompactView(0)
    .setMediaSession(mMediaSession.getSessionToken())
    // These two lines are only required if your minSdkVersion is <API 21
    .setShowCancelButton(true)
    .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
      this, PlaybackStateCompat.ACTION_STOP)));

  return builder.build();
}
 
Example #19
Source File: BackgroundAudioService.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent);
    return super.onStartCommand(intent, flags, startId);
}
 
Example #20
Source File: PlayerService.java    From blade-player with GNU General Public License v3.0 4 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    MediaButtonReceiver.handleIntent(mSession, intent);
    return START_STICKY;
}
 
Example #21
Source File: MediaNotificationManager.java    From android-MediaBrowserService with Apache License 2.0 4 votes vote down vote up
private NotificationCompat.Builder buildNotification(@NonNull PlaybackStateCompat state,
                                                     MediaSessionCompat.Token token,
                                                     boolean isPlaying,
                                                     MediaDescriptionCompat description) {

    // Create the (mandatory) notification channel when running on Android Oreo.
    if (isAndroidOOrHigher()) {
        createChannel();
    }

    NotificationCompat.Builder builder = new NotificationCompat.Builder(mService, CHANNEL_ID);
    builder.setStyle(
            new MediaStyle()
                    .setMediaSession(token)
                    .setShowActionsInCompactView(0, 1, 2)
                    // For backwards compatibility with Android L and earlier.
                    .setShowCancelButton(true)
                    .setCancelButtonIntent(
                            MediaButtonReceiver.buildMediaButtonPendingIntent(
                                    mService,
                                    PlaybackStateCompat.ACTION_STOP)))
            .setColor(ContextCompat.getColor(mService, R.color.notification_bg))
            .setSmallIcon(R.drawable.ic_stat_image_audiotrack)
            // Pending intent that is fired when user clicks on notification.
            .setContentIntent(createContentIntent())
            // Title - Usually Song name.
            .setContentTitle(description.getTitle())
            // Subtitle - Usually Artist name.
            .setContentText(description.getSubtitle())
            .setLargeIcon(MusicLibrary.getAlbumBitmap(mService, description.getMediaId()))
            // When notification is deleted (when playback is paused and notification can be
            // deleted) fire MediaButtonPendingIntent with ACTION_STOP.
            .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(
                    mService, PlaybackStateCompat.ACTION_STOP))
            // Show controls on lock screen even when user hides sensitive content.
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    // If skip to next action is enabled.
    if ((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS) != 0) {
        builder.addAction(mPrevAction);
    }

    builder.addAction(isPlaying ? mPauseAction : mPlayAction);

    // If skip to prev action is enabled.
    if ((state.getActions() & PlaybackStateCompat.ACTION_SKIP_TO_NEXT) != 0) {
        builder.addAction(mNextAction);
    }

    return builder;
}