B4A Library Audio library v1.5 - New AudioStreamer object

Status
Not open for further replies.
This version includes a new AudioStreamer type. Note that this is a beta version (for this type only).

The purpose of AudioStreamer is to make it simple to stream audio from the microphone and to the speakers. Internally it is based on AudioTrack and AudioRecord.

Next week I plan to create an example of streaming audio between two devices (walkie-talkie app).

Recording is done by calling StartRecording. The RecordBuffer event will be raised multiple times during the recording:
B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
   'collect the recording data
   buffers.Add(Buffer)
End Sub
You can write this data to a file or as done in this example, collect the buffers in a list. Later we will use this list to play the recording:
B4X:
Sub btnPlay_Click
   btnStartRecording.Enabled = False
   streamer.StartPlaying
   For Each b() As Byte In buffers
      streamer.Write(b)
   Next
   streamer.Write(Null) 'when this "message" will be processed, the player will stop.
End Sub
Note that the player has an internal queue. The write method will not block the main thread. The data is just added to the queue. It is processed by another thread.

SS-2013-06-26_17.07.34.png
 

Attachments

  • AudioStreamerExample.zip
    10.4 KB · Views: 1,483
Last edited:

Nizze

Active Member
Licensed User
Longtime User
Great !!

To be able to do a walkie-talkie app is a great thing that i have been waiting for a long time to be reality

Br
Nizze
 

drachmad

Member
Licensed User
Longtime User
Hi Erel, it is a nice feature.
I try to download the example but it seem it is not format properly (the Audio.zip is ok)
Using Chrome like it will never finish, using firefox with Fdm, I can not unzip it.
And btw the size seem like around 30 KB while in you thread it should be around 7.7 KB?
Thanks.
 

drachmad

Member
Licensed User
Longtime User
the download name is:
18081d1372255712-audio-library-v1-5-new-audiostreamer-object-audiostreamerexample.zip
if it helps.
 

drachmad

Member
Licensed User
Longtime User
Now its working, thanks.
Your sample works perfectly on Galaxy Tab II 7.
It give message in the log:
sending message to waiting queue (streamer_recordbuffer)
if the screen is going to sleep mode and it stop recording.
I think it is nicer if there is a sound it will still recording until stop recording is pressed
or keep the screen alive while the stop recording is not pressed.

It is possible to give an example to save the stream to a file in the example?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is just a simple example. It is better to move the recording code to a service.

In order to save the stream to a file you should open an OutputStream with File.OpenOutput and then write the bytes with OutputStream.WriteBytes. When the record is complete you should close the file.

Note that the file will not be a valid wav file because it doesn't include the wav headers.

To play the file you need to open an InputStream and then read chunks of data from the file. The buffer size used for writing should not be larger than streamer.PlayerBufferSize.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The attached example saves the recording to a wave file. First it creates the wave header. Then the data is saved and the last step is to write two additional fields that depend on the data size.

This time we use MediaPlayer to play the recorded file.

This is a good example of using RandomAccessFile to implement a specific file format.
 

Attachments

  • RecordToWaveFile.zip
    8.2 KB · Views: 2,383

drachmad

Member
Licensed User
Longtime User
Hi Erel, thanks for the sample.
When you wrote just add the WAV header, I am lost because all in the B4A has only library for WAV file.
AudioRecorder Library V1.00 by XverhelstX
AudioRecord Library V1.01 by stevel05
Now I think a newbie can see the structure of the WAV header.
I have change your sample into a service may be it is a good start for a newbie like me to experience with the lib. So that it is easier to combine with other application that needs recording.

Now the only problem is converting/recording to MP3. It seem in B4A its still not mature the libs or classes.
 

Attachments

  • RecordToWaveFileServ.zip
    8.9 KB · Views: 1,253

drachmad

Member
Licensed User
Longtime User
Hi moster67, thanks.
I have read that, he himself wrote it is not stable. Thats why I wrote is not mature yet. But thanks anyway.
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
This version includes a new AudioStreamer type. Note that this is a beta version (for this type only).

The purpose of AudioStreamer is to make it simple to stream audio from the microphone and to the speakers. Internally it is based on AudioTrack and MediaRecorder.

Next week I plan to create an example of streaming audio between two devices (walkie-talkie app).

Recording is done by calling StartRecording. The RecordBuffer event will be raised multiple times during the recording:
B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
   'collect the recording data
   buffers.Add(Buffer)
End Sub
You can write this data to a file or as done in this example, collect the buffers in a list. Later we will use this list to play the recording:
B4X:
Sub btnPlay_Click
   btnStartRecording.Enabled = False
   streamer.StartPlaying
   For Each b() As Byte In buffers
      streamer.Write(b)
   Next
   streamer.Write(Null) 'when this "message" will be processed, the player will stop.
End Sub
Note that the player has an internal queue. The write method will not block the main thread. The data is just added to the queue. It is processed by another thread.

SS-2013-06-26_17.07.34.png


As this is a beta version, it will be great if you can run the small example and test it on your device...

An example of saving the recording to a wave file is available in post #10.
Walkie-Talkie example is available here.


Installation instructions:
This library should be copied to the internal libraries folder.

Unzip the attached file and copy both files to (default):
C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries

something must be wrong with me, please do not be angry...
i still can't figure out how to read a magnetic card using a card reader attached to the audio jack - i must be missing something and it's killing me
please help
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Have you tried to record the "audio" while the card reader is working?
no i did not as i don't need the recorded audio, i need the text data from the magnetic stripe of the card - the card info
will it store this data in the file as a text string when i "read/record" the card ?
thanks so much for your replies and assistance
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Erel,
did try the audio samples which worked perfectly, recorded my voice and play it.
i guess it will work when the mic is external and plugged to the audio jack
however - when going to capture the credit card data i need to have the data only without any "audio" etc
and i need it not as audio as it is not - it is a text string
so i am lost again
any possible sample of capturing credit card data using a reader plugged into the audio jack ?
thanks once again
 

js1234

Member
Licensed User
Longtime User
I'm copy Audio.jar and Audio.xml to C:\Program Files\Anywhere Software\Basic4android\Libraries
but library isn't visible in Library tab!
why?
 
Status
Not open for further replies.
Top