Android Question queue-lenght in AudioStreamer

skrjabin

Active Member
Licensed User
How can I see the current queue-lenght in a MediaPlayer AudioStreamer?
The PlayerBufferSize only showes me the general maximal lenght, not how many bytes are in it now, but not played yet. Also the _PlaybackComplete event is never fired.

B4X:
Dim streamer As AudioStreamer
Dim iMax As Int = streamer.PlayerBufferSize
streamer.Write(bData) 'how many bytes can I put now?
streamer.StartPlaying
 

skrjabin

Active Member
Licensed User
Some understanding problems: I have a SAMPLERATE 44.100 and 2 bytes per Sample. So if the PlayerBufferSize is 7072, I can buffer (7072 / 44.100 / 2 * 1000) about 80 milliseconds, right? And with this test I should get the count = 3, because after 4 loops the buffer should include allready 8000 bytes. The result bAdded should than be False.
B4X:
Dim bTest(2000) As Byte
   Dim bAdded As Boolean = True
    Dim iCount As Int
    as_Beats.StartPlaying
    Dim iMax As Int = as_Beats.PlayerBufferSize '7072
    Do While bAdded = True
        bAdded = as_Beats.Write(bTest)
        iCount = iCount + 1
    Loop
    Log(iCount)
But the result is always True. Where is my mistake?
 

skrjabin

Active Member
Licensed User
Thanks for the response. My app wants to create sound in realtime depending on the user behavior. So if the queue contains allready too many bytes before the .write(newdata), it is not realtime, because this bytes will be played before. But if I use write too late, the queue is empty and it will be a gab. So the best idea for me would be, to get the current queue size. Is that possible or is there another way for me?
 

stevel05

Expert
Licensed User
What sort of sound(s) are you creating? There may be a better method of achieving what you want to do.
 

skrjabin

Active Member
Licensed User
For example an kind of music instrument app, that is nonstop producing sound with self calculated sinus waves. The frequencies depends on accelerator-data. So the user can "play" and manipulate the sound with the phone, just moving it. I thought the audiostreamer is the best way for doing that. Is there a better solution?
 

stevel05

Expert
Licensed User
The Audiostreamer Lib is designer for playing a stream of audio. If you are generating the audio on the fly It would make more sense to use the AudioTrack Object directly. There is a library https://www.b4x.com/android/forum/threads/audiotrack.14007/

Although the audio streamer uses Audiotrack to play the sound, not all of the available methods are exposed.

The other option would be to generate some sine wav's and use soundpool to play them. The wav's can be pitch shifted (although this changes the length of the sound) and looped (negating the length issue for a sine wave). If you generated 3 or 4 wav's per octave, it would probably be sufficient. You could also easily play more than one sound at a time.
 

skrjabin

Active Member
Licensed User
Thank you, AudioTrack seems to be a good solution than. I will test it and than report here.
 
Top