Android Question can BA raise events to a service? (solved)

wbtcpip

Member
Licensed User
Longtime User
I have a java library with the following code:


B4X:
  // Encoder callback
  @Events(values={"EncodeProc(int handle, int channel, ByteBuffer buffer, int length, Object user)"})
    @ShortName("BASSEnc_EncodeProc")
    public static class EncodeProcedure {
        private BA ba;
        private String eventName;
        public void Initialize(BA ba, String EventName) {
             this.ba = ba;
             this.eventName = EventName.toLowerCase(BA.cul);
        }
        public BASSenc.ENCODEPROC encodeproc = new BASSenc.ENCODEPROC() {   
                 public void ENCODEPROC(int handle, int channel, ByteBuffer buffer, int length, Object user) {
                                 byte[] b = new byte[length];
                                    buffer.get(b);
                                    ba.raiseEvent(null, eventName + "_encproc", handle, channel, b, length, user);           
                  }
       };
    }


    static {
        System.loadLibrary("bassenc");
    }

And in my B4A service:

B4X:
Sub Process_Globals
    ....
       Dim Pencopus As BASSEnc_EncodeProc
    .....
End Sub
Sub Service_Create
    .....
    Pencopus.Initialize("myenc")
    .....
    Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Vibrate = False
    Service.StartForeground(1, Notification1)
End Sub

but when i start the service i get:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (audioservice) Create ***
Error occurred on line: 31 (AudioService)
java.lang.ClassCastException: anywheresoftware.b4a.objects.NotificationWrapper$NotificationData cannot be cast to android.app.Notification
at bass.example.audioservice._service_create(audioservice.java:411)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:738)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at bass.example.audioservice.onCreate(audioservice.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3874)
at android.app.ActivityThread.access$2100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7410)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Service (audioservice) Start **

If i put the same code in activity it works without problems. Is BA object incompatible with service?
 

walterf25

Expert
Licensed User
Longtime User
WHere are you declaring your Notification variable, if you declare it in the Sub Process Globals it won't work, you need to declare it inside the Sub Service_Create.

Walter
 
Upvote 0

wbtcpip

Member
Licensed User
Longtime User
Thank you walterf25. I moved the Notification variable in Sub Service_Create
B4X:
Sub Service_Create
    'LibBass.BASS_Init(-1,44100,0)
    Dim Notification1 As Notification
    BassUser.Initialize("fup")
    Pencopus.Initialize("myenc")
    Socket1.Initialize("Socket1")
    Socket2.Initialize("Socket2")
    Notification1.Initialize
    Notification1.Icon = "icon" 'use the application icon file for the notification
    Notification1.Vibrate = False
    Service.StartForeground(1, Notification1)
End Sub

but still the error is raised:

** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (audioservice) Create ***
Error occurred on line: 32 (AudioService)
java.lang.ClassCastException: anywheresoftware.b4a.objects.NotificationWrapper$NotificationData cannot be cast to android.app.Notification
at bass.example.audioservice._service_create(audioservice.java:414)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:738)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:357)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:260)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
at bass.example.audioservice.onCreate(audioservice.java:56)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3874)
at android.app.ActivityThread.access$2100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1909)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7410)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
** Service (audioservice) Start **
 
Upvote 0

wbtcpip

Member
Licensed User
Longtime User
now i fear that a service has not a message queue so i will not able to receive any event from BA. The activity go to background, so i cannot use it for encoding audio data in the background, i need a way to receive events from BA inside a service. I'm lost!
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I think you are confusing things...
so i will not able to receive any notification from BA
B4A does not send any Notification. You create the notificationobject and give it to Android.
Android then will call the receiver which belongs to the notifiction if the user press it.
At this point the Service or Activity which you defined will be started.

so i cannot use it for encoding audio data in the background
How is a Notification related to audio encoding in the background?

If you want to encode audio then use a service to do the job.
 
Upvote 0

wbtcpip

Member
Licensed User
Longtime User
DonMAnfred sorry if sometimes im using uncorrect words. What i want do is to receive this callback:

ba.raiseEvent(null, eventName + "_encproc", handle, channel, b, length, user); (this is an array of encoded audio data form a java library)

into a service of my application.

Note that i receive the callback without problems inside the activity but now i moved my relevant code into a service to do the job and i get this java error:

java.lang.ClassCastException: anywheresoftware.b4a.objects.NotificationWrapper$NotificationData cannot be cast to android.app.Notification

as you can see in my first post i'm already using a service and that's why i have started the post.

if i don't use a service and use the activity everything works well (until the activity is in foreground of course)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
into a service of my application
so in the service you should use the Object of your lib. dim x as y. x.initialize("Eventname",...)
Also you should create a sub for the event in the service

B4X:
sub EventName_encproc(your parameters)
end sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@Events(values={"EncodeProc(int handle, int channel, ByteBuffer buffer, int length, Object user)"})
this is wrong.

It must be declared in B4X Notation...

@Events(values={"EncodeProc(handle As Int, channel As Int, buffer as Object, length As Int, user As Object)"})
I´m not sure what ByteBuffer is so i declared it as Object.
 
Upvote 0

wbtcpip

Member
Licensed User
Longtime User
This is the sub in b4a that receive that callback

Sub myenc_encproc(handle As Int, channel As Int, mybuffer() As Byte, length As Int, User As Object)
If Socket2.Connected Then
astream2.Write2(mybuffer,0,length)
'Log(DateTime.Time(DateTime.Now) & " Sent " & length & " bytes to MBStudio")
End If
End Sub

It's working fine if i run the code in an activity, while java gives error if i move all my code in a service.
As i'm receiving live audio data i need to be working in a service but unfornately java raise the error
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
If you add the @ActivityObject in you library, it will not run in a Service, if you want it to run inside a Service then you need to get rid of the @ActivityObject part.

Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…