Android Question Notification - audio play / pause button

Jack Cole

Active Member
Licensed User
Is there a way to have a play/pause button like Audible has in the notification? In the screenshot below, I show the closest thing I could find with the nb6 class. But it requires you to pull down the notification to show the actions. Contrast with the Audible notification above that.

1580008628670.png
 
Last edited:

Erel

Administrator
Staff member
Licensed User


Example code:
B4X:
Sub MediaStyle_Notification
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "DEFAULT").SmallIcon(smiley)
    n.AddButtonAction(smiley, "Action 1", MyService, "action 1") 'use different bitmaps...
    n.AddButtonAction(smiley, "Action 2", MyService, "action 2")
    n.AddButtonAction(smiley, "Action 3", MyService, "action 3")
    n.MediaStyle
    n.Build("Actions", "Actions", "tag", Me).Notify(1)
End Sub
Add to NB6:
B4X:
'Creates a media style notification
Public Sub MediaStyle As NB6
    If IsBuilder Then
        SetStyle("android.app.Notification$MediaStyle", CreateMap("setShowActionsInCompactView": Array As Int(0, 1, 2)))
    End If
    Return Me
End Sub
 

wes58

Active Member
Licensed User
Just a note, that example shown by Erel in post #2 is not a full android Notification.MediaStyle as described there - https://developer.android.com/reference/android/app/Notification.MediaStyle

Notifications created with MediaStyle will have their category set to Notification#CATEGORY_TRANSPORT unless you set a different category using Notification.Builder#setCategory(String).

Finally, if you attach a MediaSession.Token using setMediaSession(MediaSession.Token), the System UI can identify this as a notification representing an active media session and respond accordingly (by showing album artwork in the lockscreen, for example).

To use this style with your Notification, feed it to Notification.Builder#setStyle(android.app.Notification.Style) like so:

Notification noti = new Notification.Builder()
.setSmallIcon(R.drawable.ic_stat_player)
.setContentTitle("Track title")
.setContentText("Artist - Album")
.setLargeIcon(albumArtBitmap))
.setStyle(new Notification.MediaStyle()
.setMediaSession(mySession))
.build();
 
Top