Android Question Cannot get AudioStream to Initialize

jmeuse2

Member
Licensed User
Longtime User
Ive downloaded the IDE recently, Ive tried many AudioStream. Initialize parameters but none work. Can anyone help? Ive tried AudioStream.Initialize("AudioStream") & AudioStream.Initialize() & AudioStream.Initialize. ChatGpt gave up the ghost. Java, Audio & AudioRecord are checked in the library. Something Im missing, Im trying to show sound levels from a microphone.

Sub Process_Globals
Dim AudioStream As AudioStreamer
Dim Buffer(2048) As Byte ' Buffer size for audio data
End Sub

Sub Globals
Private Timer1 As Timer
Private lblVolume As Label ' Label to display sound levels
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Main") ' Load layout with lblVolume

'If FirstTime Then
' Initialize the AudioStreamer
AudioStream.Initialize'(AudioStream) '(AudioStream) ' Event handler name
AudioStream.SampleRate = 44100 ' Set sample rate (44.1 kHz)
AudioStream.Mono = True ' Use mono audio
AudioStream.StartRecording ' Start recording audio
'End If

' Set up a timer to process the audio data at regular intervals
Timer1.Initialize("Timer1", 200) ' Every 200ms
Timer1.Enabled = True
End Sub

Sub Timer1_Tick
' Read audio data into buffer
Dim ReadBytes As Int = AudioStream.Read(Buffer, 0, Buffer.Length)

If ReadBytes > 0 Then
Dim Sum As Long = 0

' Process the audio buffer (taking every two bytes as a sample)
For i = 0 To ReadBytes - 1 Step 2
Dim Sample As Short = Bit.Or(Bit.And(0xff, Buffer(i)), Bit.ShiftLeft(Bit.And(0xff, Buffer(i + 1)), 8))
Sum = Sum + Abs(Sample) ' Accumulate absolute values of the samples
Next

' Calculate the amplitude (average absolute value of the samples)
Dim Amplitude As Float = Sum / ReadBytes
' Calculate decibels (20 * log10(Amplitude))
Dim Decibels As Float = 20 * (Log(Amplitude) / Log(10))

' Display the decibel value on the label
lblVolume.Text = "Volume: " & NumberFormat2(Decibels, 1, 2, 2, False) & " dB"
End If
End Sub
 
Last edited:

Alexander Stolte

Expert
Licensed User
Longtime User
there is an example project in the official libraries thread:
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
Thanks for that! I needed to change to an older Java version to help it out, JDK11. Never ending particularities.
 
Upvote 0

emexes

Expert
Licensed User
I started plugging your code into a B4A project here, and immediately fell over your .Initialize call:

AudioStream.Initialize'(AudioStream) '(AudioStream) ' Event handler name
AudioStream.SampleRate = 44100 ' Set sample rate (44.1 kHz)
AudioStream.Mono = True ' Use mono audio
AudioStream.StartRecording ' Start recording audio

and how the IDE pop-up help says to do the .Initialize call:

1744182432138.png



Are you using B4A (not B4I or B4J) and using the built-in internal Audio library? (shows as version 1.71 here, but any version should work)

Or are you using some third-party replacement library?
 
Upvote 0

emexes

Expert
Licensed User
Also, recording needs Runtime Permission:

B4X:
Dim rp As RuntimePermissions
rp.checkandRequest(rp.PERMISSION_RECORD_AUDIO)
wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
I started plugging your code into a B4A project here, and immediately fell over your .Initialize call:



and how the IDE pop-up help says to do the .Initialize call:

View attachment 163301


Are you using B4A (not B4I or B4J) and using the built-in internal Audio library? (shows as version 1.71 here, but any version should work)

Or are you using some third-party replacement library? What do you mean by fell over?
Yes I'm using B4A and the Audio version is 1.71.
 
Upvote 0

emexes

Expert
Licensed User
Yes I'm using B4A and the Audio version is 1.71.

Righto, I'll assume the departure from the documentation is that you've ported across sample code from Java or similar.

I'll give it a burl after dinner, unless you get it going in the meantime.
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
Thanks, it's a copied snippet. Just not Java compatible or something. Need Java version and correct libraries etc.
 
Upvote 0

emexes

Expert
Licensed User
Does it need a special Java version to compile?

I did nothing special. Stock-standard B4A install, with whatever Java version the installation instructions said to use.

If you can run the B4A new program template, with the button that brings up a hello world message, then you should be fine.
 
Upvote 0

jmeuse2

Member
Licensed User
Longtime User
No “Save As” and the IDE config survives uninstalls unless they're manually purged, won't export as a Zip, this that and my shoe size at Foot Locker. The IDE I remembered working with 10 years ago didn't have me fight so hard to get something as simple as Tabs to work, etc.
 
Upvote 0
Top