Android Question Why does AudioStreamer.Write() work with arrays bigger than the PlayerBufferSize

Rodrigo Muñoz

Member
Licensed User
Longtime User
Hi everyone,

I took Erel's RecordToWave example, and replaced the btnPlay_Click subroutine as shown below. In the DirRootExternal directory of my device I have a WAV file called mFileName, which I read into array B() of bytes. Then I just write that array to the AudioStreamer. Everything works, in spite of array B() being much bigger than the PlayerBufferSize (4096 in my device).

I'm confused because the documentation says one should not pass arrays bigger than the PlayerBufferSize. Thanks in advance for any clarifications.

Rod

B4X:
Sub btnPlay_Click

Dim INSTR As InputStream, NumBytes As Long

'Read bytes of WAV file into array B() (only audio data; WAV header dropped)
NumBytes = File.Size(File.DirRootExternal, mFileName) - 44  'Number of audio bytes in WAV file (excluding header)
INSTR = File.OpenInput(File.DirRootExternal, mFileName)      'Open WAV file for input
Dim B(44) As Byte                                            'Read first 44 bytes (header)
INSTR.READBYTES(B, 0, B.length)
Dim B(NumBytes) As Byte                                      'Read all remaining bytes (audio data) into array B()
INSTR.READBYTES(B, 0, B.length)

streamer.StartPlaying
streamer.Write(B)          'This works in spite of B() being much bigger than the PlayerBufferSize
streamer.Write(Null)

End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is safer to split the data based on the buffer size. There is also a mistake in your code, you should not assume that InputStream.ReadBytes will read all the bytes at once.

The correct code is:
B4X:
Dim bc As ByteConverter
streamer.StartPlaying
Dim B(44) As Byte
INSTR.READBYTES(B, 0, B.length)
Dim B(streamer.PlayerBufferSize) As Byte
Dim count As Int = INSTR.READBYTES(B, 0, B.length)
Do While count > 0
  Dim Data(count) As Byte
  bc.ArrayCopy(B, 0, Data, 0, count)
 streamer.Write(Data)
 count =  INSTR.READBYTES(B, 0, B.length)
Loop
streamer.Write(Null)
 
Upvote 0

Rodrigo Muñoz

Member
Licensed User
Longtime User
Thank you very much Erel. The first code I did was similar to yours, but since the simpler version (with no loops) also worked well, I had to ask. Perhaps the simpler version would crash in other machines, or with a bigger file?
 
Upvote 0
Top