Android Question AudioStreamer & Service

Midimaster

Active Member
Licensed User
I use Erel's code sample "beep" to create code playing audio via the AudioStreamer. It works already, but now I had a crash without any debug feedback, the smartphone hang. So I asked me, whether my way is already correct. On the other side I do not really understand the co-operation of MAIN() and STARTER().

Every second I tranfer new Bytes to the already open AudioStreamer.

that is what I'm doing:
MAIN:
...

Sub SendMoreSamples
    ....
    Private AudioOutBytes() As Byte
    ' fill them
    ....
    CallSub2(Starter, "SendToAudio", AudioOutBytes)
End Sub


STARTER:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region



Sub Process_Globals
    Public AudioOut As AudioStreamer
End Sub



Sub Service_Create
    AudioOut.Initialize("AudioOut",22100,True,16,AudioOut.VOLUME_MUSIC)
    AudioOut.StartPlaying
End Sub



Public Sub SendToAudio(Bytes() As Byte)
    AudioOut.Write(Bytes)
End Sub



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



Sub Service_TaskRemoved
End Sub


Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub



Sub Service_Destroy
End Sub

Now my Questions are:

1. Do I need to stop the AudioStreamer, when I finish the app? For me it is important to keep the player open during app is open. So I cannot add a NULL byte at the end of the transfered Byte()-Array like Erel suggested in his example.

2. And where can I add the code for stopping it?

3. How can I react on Events from the AudioStreamer? Which events are avaiable? Why can I not see the possible Event-related Subs of the AudioStreamer in my code? It looks like the AudioStreamer is unknown in my MAIN.
 

JohnC

Expert
Licensed User
Longtime User
I don't know what type of sound you are playing, but if you only want to play a simple sound file, I would recommend SoundPool:

 
Upvote 0

Midimaster

Active Member
Licensed User
I think I did not write anything about a simple sound file.... Its a project where the app plays 20 instrument tracks simultan. Each track is 4-5min and 10-20MB. the user can switch on/off and adjust the volume of each instrument in real time.

It is already working, time consumption and memory are no problems any more. Now I only need to know, what to do with the AudioStreamer object, when leaving or pausing the app.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
On the other side I do not really understand the co-operation of MAIN() and STARTER().
Have you read these (and watched the video)?
1) https://www.b4x.com/android/forum/threads/android-process-and-activities-life-cycle.6487/
2) https://www.b4x.com/android/forum/threads/service-modules.7542/#content
3) https://www.b4x.com/android/forum/t...-consistent-single-entry-point.57599/#content
BTW, some of that is in the process of changing because of
1) https://www.b4x.com/android/forum/t...k-for-managing-multiple-pages.118901/#content

For me it is important to keep the player open during app is open
And where can I add the code for stopping it?
Instead of starting the playing in the Starter, you could start playing in Activity_Resume (open) and stop in Activity_Pause (close)
It looks like the AudioStreamer is unknown in my MAIN.
Because you declared it in the Starter?
Which events are avaiable?
To see events available, type
B4X:
sub
followed by a space and then a tab. A box should box up with the types available (dependent on libraries active/selected in the libraries tab). Pick a type and then the events available list should show up. Might want to read some of the documentation on the B4X IDE found here: https://www.b4x.com/android/documentation.html

BTW, welcome!
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
Not about your problem, but I think, it is better to use AudioStreamer in a Class, not in a Service. So you can reach it from everywhere with .SendToAudio, no need for CallSub.
And yes, you should close the AudioStreamer in Activity_Pause.
 
Upvote 0
Top