Android Question Music-Player NB6

kisoft

Well-Known Member
Licensed User
Longtime User
Hi
I'm using this example.
he service code looks like this:
B4X:
Sub Process_Globals
    Private nid As Int = 1
    Private lock As PhoneWakeState
    Private pl As SimpleExoPlayer
End Sub

Sub Service_Create
    lock.PartialLock
    pl.Initialize("pl")
End Sub


Sub Service_Start (StartingIntent As Intent)
    Service.StopAutomaticForeground
End Sub

Public Sub Play
    Service.StartForeground(nid, CreateNotification("Playing music - Radio Paradise"))
    pl.Prepare(pl.CreateUriSource("http://stream.radioparadise.com/aac-128"))
    pl.Play
End Sub

Sub pl_Error (Message As String)
    Log(Message)
End Sub

Sub pl_Complete
    Log("complete")
End Sub

Public Sub Stop
    Service.StopForeground(nid)
    pl.Pause
End Sub


Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Tracking location", Body, Main)
    Return notification
End Sub

Sub Service_Destroy
    pl.Pause
    lock.ReleasePartialLock
End Sub

How can I add action buttons (pause, play) to this notification? Or how to replace this notification with NB6 class? Or how to integrate NB6 class with this service?
 

DonManfred

Expert
Licensed User
Longtime User
 
Upvote 0

pazzokli

Active Member
Licensed User
Longtime User
Simply replace your notification with NB6 class and use that SUB:
B4X:
Sub Notification_WithActions
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "DEFAULT").SmallIcon(smiley)
    n.AddButtonAction(smiley, "Action 1", MyService, "action 1")
    Dim cs As CSBuilder
    n.AddButtonAction(Null, cs.Initialize.Color(Colors.Red).Bold.Append("Action 2").PopAll, MyService, "action 2")
    n.DeleteAction(MyService, "delete action")
    n.Build("Actions", "Actions", "tag", Me).Notify(1)
    
End Sub

Remember to import "MyService" also
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
NB6.Build returns a regular notification. You should pass it to Service.StartForeground.
Yes ... but I don't know how to do it correctly. I do it's just wrong ...
B4X:
Sub Service_Start (StartingIntent As Intent)
    If StartingIntent.IsInitialized Then
        Dim cs As CSBuilder
        cs.Initialize.Bold.Size(20).Append($"Action: ${StartingIntent.Action}"$).PopAll
        Log(cs)
    If StartingIntent.Action="Pause" Then  Stop
    If StartingIntent.Action="Play" Then   Play
    If StartingIntent.Action="finish" Then
        Stop
        CallSub(Main,"subfinish")
        End If
    End If
    Service.StopAutomaticForeground
End Sub

Public Sub Play
    Service.StartForeground(nid, CreateNotification("Playing music - Radio Paradise"))
    pl.Prepare(pl.CreateUriSource("http://stream.radioparadise.com/aac-128"))
    pl.Play
    MediaStyle_Notification
End Sub

Sub MediaStyle_Notification
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "LOW").SmallIcon(smiley)
    n.AddButtonAction(smiley, "Action 1", Me, "Play") 'use different bitmaps...
    n.AddButtonAction(smiley, "Action 2",Me, "Pause")
    n.AddButtonAction(smiley, "Action 3", Me, "Finish")
    n.MediaStyle
    n.Build("", "", "tag", Me).Notify(1)
end sub
   
Sub CreateNotification (Body As String) As Notification
    Dim notification As Notification
    notification.Initialize2(notification.IMPORTANCE_LOW)
    notification.Icon = "icon"
    notification.SetInfo("Radio Paradise", Body, Main)
    Return notification
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Public Sub Play
    Service.StartForeground(nid, MediaStyle_Notification )
    pl.Prepare(pl.CreateUriSource("http://stream.radioparadise.com/aac-128"))
    pl.Play
    MediaStyle_Notification
End Sub

Sub MediaStyle_Notification As Notification
    Dim n As NB6
    n.Initialize("default", Application.LabelName, "LOW").SmallIcon(smiley)
    n.AddButtonAction(smiley, "Action 1", Me, "Play") 'use different bitmaps...
    n.AddButtonAction(smiley, "Action 2",Me, "Pause")
    n.AddButtonAction(smiley, "Action 3", Me, "Finish")
    n.MediaStyle
    Return n.Build("", "", "tag", Me)
end sub
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
I am getting this error
B4X:
B4A Wersja: 9.50
Java Wersja: 11
Parsowanie kodu.    (0.16s)
Building folders structure.    (0.07s)
Kompilowanie kodu.    (0.15s)
Kompilowanie kodu układu.    (0.03s)
Organizowanie bibliotek.    (0.00s)
    (AndroidX SDK)
Generowanie pliku R.    (0.72s)
Kompilowanie wygenerowanego kodu Java.    Error
B4A line: 64
Service.StartForeground(nid, MediaStyle_Notificat
src\radio\panel\player.java:258: error: incompatible types: String cannot be converted to Notification
mostCurrent._service.StartForeground(_nid,(android.app.Notification)(_mediastyle_notification()));
 
Upvote 0
Top