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


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


V1.62 is attached. It adds an Error event. This event will be raised when an error happens during recording.
New method: AudioStreamer.Initialize2.
It allows you to set the audio source. The value should be one of the values listed here: http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html
For example to set the audio source to the voice call channel you will use the value 4.
 

Attachments

  • AudioStreamerExample.zip
    10.4 KB · Views: 312
Last edited:

freedom2000

Well-Known Member
Licensed User
Not too much. See the first post.

Hi again,

I tested the 1.62 library

1) the App no longer crashes when the audio stream is already booked by another app
2) however I don't know how to trap the added event ?
3) I also tested the various sources with the intialize2 method

only DEFAULT and MIC seem to work on my Nexus 5 kitkat 4.4.2
all the other options raise an exception when startrecording occurs
 

freedom2000

Well-Known Member
Licensed User
You can handle it with:
B4X:
Sub streamer_Error
Log(LastException)
End Sub

3) Can you post the error message?
So I logged the last exception and I got :

(IllegalStateException) java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

the init itself does not raise an exception
 

danielt

New Member
Licensed User
So I logged the last exception and I got :

(IllegalStateException) java.lang.IllegalStateException: startRecording() called on an uninitialized AudioRecord.

the init itself does not raise an exception

Hi Erel and co. Just wondering if anyone has a solution for this? I'm also hoping to use a Bluetooth headset microphone as the input.

I get the same exception if I try to use VOICE_UPLINK (2) for example. DEFAULT and MIC work fine.
 

brelto85

Active Member
Licensed User
with the version 1.62, is possible to set the AudioSource to VOICE_RECOGNITION and record/handle the voice in alternative to SpeechRecognizer library?
If so, has a sense to do?
 

freedom2000

Well-Known Member
Licensed User
Make sure to use the latest Audio library as it allows you to handle this error instead of crashing.

This error means that Android failed to open this channel. There isn't much that can be done about it.
I can confirm that the 1.62 version of the lib handles properly this exception

And that it is really dependant on the device you are using, some accept more audio channels than others... That's life !
 

DRSolomon

New Member
Licensed User
Greetings people,

Working with the videorecord aspect and all is well, however I need to be able to turn the flash of programmatically..... Anyone got a snippet for me in this regard please..... Any working example?
I'm also looking for a means to get the ambient light level from the light sensor on the phone, any ideas?

Many thanks
 

Rick Harris

Well-Known Member
Licensed User
May I suggest that you change the title of this thread (or create a new thread) to indicate the existence of Audio library version 1.62? When using the Forum search feature Audio library version 1.6 does not show up!
 
Last edited:

Rick Harris

Well-Known Member
Licensed User
Hi Erel and co. Just wondering if anyone has a solution for this? I'm also hoping to use a Bluetooth headset microphone as the input.

I get the same exception if I try to use VOICE_UPLINK (2) for example. DEFAULT and MIC work fine.
I had the same problem, but when I initialized the streamer under Activity_create (in stead of in a sub) this problem went away.
 

Rick Harris

Well-Known Member
Licensed User
Each item in this element is a Byte (value between -128 to 127).
If anybody wants to create a VU meter that shows the input speech level while recording, then use this:
B4X:
Sub streamer_RecordBuffer (Buffer() As Byte)
    If recording Then output.WriteBytes(Buffer, 0, Buffer.Length)
    Dim VUlevel as int
    VUlevel = ABS(Buffer(1))    
End Sub

The VUlevel uses the MSD in case of a 16 bit WAV and varies between 0 and 127.
It can drive a progress bar or a nicer looking meter of your own design.
 
Last edited:
Status
Not open for further replies.
Top