B4A Class Audio Visualizer

stevel05

Expert
Licensed User
As a response to a request on the forums, and to try out the new JavaObject functionality, I decided to take a diversion and write a wrapper for the http://developer.android.com/reference/android/media/audiofx/Visualizer.html.

It's written as a B4a class using JavaObject, and provides the DataCaptureListener and callbacks for wave and fft data.

Just add an audio file, and some processing code.

The Class depends on JavaObject and the Demo App depends on Reflection and JavaObject.

You need to add permission: RECORD_AUDIO to the Manifest.

B4X:
AddPermission("android.permission.RECORD_AUDIO")
If you want to apply the visualizer to the main outputs (AudioSession = 0) you also need permission: MODIFY_AUDIO_SETTINGS

B4X:
AddPermission("android.permission.MODIFY_AUDIO_SETTINGS")
Requires B4A 3.80+

The Class depends on JavaObject and the Demo App depends on Reflection and JavaObject.

Enjoy

Update: Added a new version with WavForm drawing to a user provided panel. While I have tested it on various configurations, it needs to be tested further before production use. Please let me know if you come across any issues.

This version has additional dependencies:

SLByteArrayBuffer and Threading Libraries.

Edit: I forgot to mention that as the Wav drawing is done in a separate thread, it won't run in Debug mode. You can disable it by temporarily removing the TH.Start call to the draw routine, better still use conditional compilation.

If you want to debug the rest of the app, add

B4X:
#ExcludeFromDebugger: True
to the top of the WavDraw class, then you can run the app in debug mode, but you can't debug the class. In legacy debug mode the wav will be drawn, in Rapid debug mode it won't, but the app will still run.

As an aside, I know nothing about using fft, if someone would care to provide an example using that, it would be most appreciated.

Edit: 20/1/16 Uploaded new Vizualizer.bas Class which fixes the bug from post #29
26/3/18 Update to fix the bug in post #29
 

Attachments

Last edited:

stevel05

Expert
Licensed User
Updated to add waveform drawing to a panel.
 

fanfalveto

Active Member
Licensed User
Hi,i have a problem with samsung galaxy s4 device.
i do in Main declare and initialize a Mediaplayer. (so1)
then
Sub Button1_Click
Try
Dim booo As String=dat.bo1
Catch
End Try
Try
If so1.IsPlaying Then
Button1.color=Colors.red
so1.stop
va1=0
Else
Button1.color=Colors.Green
Button1.TextColor=Colors.Black

If booo=1 Then
caan =dat.li1
Log (caan)
Try
so1.Load(File.DirRootExternal&"/soundfx/ritm/",caan)
Catch
so1.Load("/storage/extSdCard/soundfx/ritm/",caan)
End Try
so1.play
so1.Looping=True
Else
so1.Load(File.DirAssets,"1.wav")
so1.play
so1.Looping=True
End If
End If
Catch
End Try
End Sub
and into other activity your visualizer-wavform.
i load a song and mediaplayer play rigth from File.DirRootExternal&"/soundfx/ritm/" but i can´t see the graphics.
but from File.DirAssets i can see.
any idea why? thank you
 

stevel05

Expert
Licensed User
Can you zip and post your project, I can't see where you've initialized the visualizer , or the wave form draw in the code you've posted.

The visualizer modules should be in the same activity as the mediaplayer, if you're not sure, it's better to start with the demo project and change that to suit your needs.
 

fanfalveto

Active Member
Licensed User
please,how i can visualizer 2 songs at same time,one in green and the other in white for example.
i try to create another viz object but crash.
any idea?
thank you.
 

stevel05

Expert
Licensed User
How are you playing your two songs? Two Mediaplayer objects?
 

stevel05

Expert
Licensed User
You need to create two of everything (including panels). And delay the start of subsequent drawing routines.

Try this one.
 

Attachments

stevel05

Expert
Licensed User
You had initialiszed so1 twice and not so2, and you had disabled the timer that starts the second wave draw.
 

Attachments

stevel05

Expert
Licensed User
If you want to debug the rest of your app, add
B4X:
#ExcludeFromDebugger: True
to the top of the WavDraw class, see post 1.
 

BarryW

Active Member
Licensed User
Can i used this class while recording an audio. Is short visualize while recording. Thx Masters.
 

stevel05

Expert
Licensed User
The Vizualizer is driven from the audio output, if you play the audio at the same time you are recording it, it should work, you can try setting the output volume to 0 if you don't want to hear it.
 

stevel05

Expert
Licensed User
What library are you using to record?
 

fanfalveto

Active Member
Licensed User
Hi, i try
B4X:
R.Target = tem.mp
where "tem" is a service.
The program runs but the visualizer not
B4X:
** Activity (main) Create, isFirst = true **

java.lang.NoSuchFieldException: tem.mp

    at java.lang.Class.getDeclaredField(Class.java:596)
    at anywheresoftware.b4a.agraham.reflection.Reflection.GetField(Reflection.java:316)
    at alanhm.vngsoft.music.main._activity_create(main.java:359)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at alanhm.vngsoft.music.main.afterFirstLayout(main.java:98)
    at alanhm.vngsoft.music.main.access$100(main.java:16)
    at alanhm.vngsoft.music.main$WaitForLayout.run(main.java:76)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5356)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)


    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NoSuchFieldException: tem.mp
Any idea? Thanks
 

Agnetha

Member
Licensed User
Hello!
I want to use the sub "getMeasurementPeakRms" , but then i get the error
Class "android.media.audiofx.Visualizer.MeasurementPeakRms" not found.
Can you help me? I just want to visualize the audio peaks.
 
Top