简体   繁体   中英

Change play button to pause button in notification

When I press the play button on the notification of the player I developed, I need that play button will be replaced by pause button, and vice versa. I have, in my Service class the inner Broadcast Receiver class, and the method onPrepared, where I defined the notification. In broadcast receiver I tried to do something like this(see the piece of code), but I get Null pointer exception. How can I change the play button by pause one correctly?

enter code here       case _Constants.ACTION_PLAY:
                Toast.makeText(context, "Play", Toast.LENGTH_SHORT).show();
                if(isPlaying())
                {

                        status.contentView.setViewVisibility(R.drawable.icon_pause, View.INVISIBLE);
                        status.contentView.setViewVisibility(R.drawable.icon_play, View.VISIBLE);
                        pausePlayer();



                }else {

                        status.contentView.setViewVisibility(R.drawable.icon_play, View.INVISIBLE);
                        status.contentView.setViewVisibility(R.drawable.icon_pause, View.VISIBLE);
                        go();

                }

Here's the declaration of action buttons inside of method onPrepared:

enter code here
   Intent closeIntent = new Intent(this, NotificationReceiver.class).setAction(_Constants.ACTION_EXIT);
   PendingIntent pendingCloseIntent = PendingIntent.getBroadcast(this,0 ,closeIntent,PendingIntent.FLAG_UPDATE_CURRENT);

   Intent favoriteIntent = new Intent(this, NotificationReceiver.class).setAction(_Constants.ACTION_FAVORITE);
   PendingIntent pendingFavoriteIntent = PendingIntent.getBroadcast(this, 1, favoriteIntent, PendingIntent.FLAG_UPDATE_CURRENT);

   Intent prevIntent = new Intent(this,NotificationReceiver.class).setAction(_Constants.ACTION_PREVIUOS);
   PendingIntent pendingPrevIntent = PendingIntent.getBroadcast(this,2,prevIntent,PendingIntent.FLAG_UPDATE_CURRENT);

   Intent nextIntent = new Intent(this,NotificationReceiver.class).setAction(_Constants.ACTION_NEXT);
   PendingIntent pendingNextIntent = PendingIntent.getBroadcast(this,5,nextIntent,PendingIntent.FLAG_UPDATE_CURRENT);

   Intent playIntent = new Intent(this,NotificationReceiver.class).setAction(_Constants.ACTION_PLAY);
   PendingIntent pendingPlayIntent = PendingIntent.getBroadcast(this,4,playIntent,PendingIntent.FLAG_UPDATE_CURRENT);




   Bitmap artwok = BitmapFactory.decodeResource(getResources(), R.drawable.ic_player);
    status =new NotificationCompat.Builder(this,_Constants.CHANNEL_ID)

           .setSmallIcon(R.drawable.ic_music)
           .setContentTitle(songTitle)
           .setContentText(songArtist)
           .setLargeIcon(artwok)
           .setContentIntent(pendInt)
           .setOngoing(true)

           //change then null by the required intent to make it work
           .addAction(R.drawable.ic_favorite_empty,"Mark Favorite",pendingFavoriteIntent)
           .addAction(R.drawable.icon_previous, "Previous",pendingPrevIntent)
           .addAction(R.drawable.icon_play,"Play", pendingPlayIntent)
           //.addAction(R.drawable.icon_pause,"Pause",pendingPauseIntent)
           .addAction(R.drawable.icon_next,"Next",pendingNextIntent)
           .setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
                   .setShowActionsInCompactView(1,2,3)
                   .setMediaSession(mediaSessionCompat.getSessionToken()))
           .addAction(R.drawable.ic_close, "EXIT",pendingCloseIntent)
           .setSubText("Now playing...")

           .build();


   status.flags=Notification.FLAG_ONGOING_EVENT;

   startForeground(1,status);

you can create 2 buttons play and pause in the same location in your xml file. when you clik on play set its visibility to GONE and make pause button VISIBILITY to VISIBLE and vice versa

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM