Android Question Cannot instance android.media.audiofx.visualizer - SOLVED!

artsoft

Active Member
Licensed User
Longtime User
Hi!

I want to test the audio fx visualizer example found in this forum - made by @stevel05!

Here the page:
https://www.b4x.com/android/forum/threads/audio-visualizer.41850/

But no chance to test it, cause I get always this error:

2023-05-15 10_58_50-Vizualizer - B4A.png


Current manifest is:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
AddPermission("android.permission.RECORD_AUDIO")
AddPermission("android.permission.MODIFY_AUDIO_SETTINGS")

There is no audiofx libs listed within the SDK manager.

In the screenshot above you can see that only these 2 entries are found while looking for "android.media"... :-(

And here, there is no DEPRECATION of this lib:
https://developer.android.com/reference/android/media/audiofx/Visualizer.html

What should I do?

Thanks in advance for any help.

Regards
ARTsoft
 
Last edited:

artsoft

Active Member
Licensed User
Longtime User
Please post code and logs as text.

Hi Erel!

Code is from this example:
https://www.b4x.com/android/forum/attachments/visualizer-zip.65933/

You can find it here:
https://www.b4x.com/android/forum/threads/audio-visualizer.41850/

Error-Log (see please row 211):

B4X:
** Activity (main) Create (first time) **
vizualizer_initialize (java line: 211)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance0(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
    at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:91)
    at b4a.example.vizualizer._initialize(vizualizer.java:211)
    at b4a.example.main._activity_create(main.java:372)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at b4a.example.main.afterFirstLayout(main.java:105)
    at b4a.example.main.access$000(main.java:17)
    at b4a.example.main$WaitForLayout.run(main.java:83)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
    at android.media.audiofx.Visualizer.<init>(Visualizer.java:238)
    ... 18 more
java.lang.reflect.InvocationTargetException

Java-Code:

1684159622247.png


As Text:

B4X:
public String _initialize(anywheresoftware.b4a.BA _ba,Object _module,int _audiosession) throws Exception{
innerInitialize(_ba);
 //BA.debugLineNum = 14;BA.debugLine="Public Sub Initialize(Module As Object,AudioSessio";
 //BA.debugLineNum = 15;BA.debugLine="mModule = Module";
_mmodule = _module;
 //BA.debugLineNum = 17;BA.debugLine="Viz.InitializeNewInstance(\"android.media.audiofx.";
_viz.InitializeNewInstance("android.media.audiofx.Visualizer",new Object[]{(Object)(_audiosession)});
//BA.debugLineNum = 19;BA.debugLine="End Sub";
return "";
}

This is the code of the Visualizer class:

B4X:
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Module As Object,AudioSession As Int)
    mModule = Module
    
    Viz.InitializeNewInstance("android.media.audiofx.Visualizer", Array As Object(AudioSession))
    
End Sub

I hope this will help!

Thanks a lot.
 
Last edited:
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
Steve's example works for me once I added an mp3 file as directed in the example. I didn't need to mess with SDKManager - perhaps that's what screwed it up for you.

I also added an mp3 file of my owns!
I also added other audio file formats ... but always the same issue.

My "audiosession", which is needed for vidualizer init, is valid as well.

I can see that my android.jar includes these audiofx classes:

1684162991356.png



Thanks for trying to help :)
 
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
@DonManfred + @agraham

Hi DonManfred!

Agraham could start and init the visualizer. But I dont know, which Android version agraham currently uses for the test.

It's a simple creation of a new instance - the visualizer class is included in all android.jar files.

B4X:
Viz.InitializeNewInstance("android.media.audiofx.Visualizer", Array As Object(AudioSession))

Nothing more...

Thanks

Update: Oh, I see you deleted yopur post...
 
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
Additional info while create an instance:

Parameters​


audioSession Int

System wide unique audio session identifier. If audioSession is not 0, the visualizer will be attached to the MediaPlayer or AudioTrack in the same audio session. Otherwise, the Visualizer will apply to the output mix.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
It's irrelevant whether Android.jar includes Visualizer as it is accessed at runtime by reflection in Visualizer.bas and so there is no need for it at compile time which is when Android.jar is used. By the way, Android.jar holds no code, only outline definitions for the classes and methods that should be present on the phone or tablet.

The actual problem seems to be that Visualizer is not accessible by reflection if the first install of the app targets SDK 23 or higher. I've no idea why this is. Android did restrict non-SDK access by reflection from SDK 28 onwards but SDK 23 does of course predate this restriction. However it does look like you are out of luck here.
 
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
It's irrelevant whether Android.jar includes Visualizer as it is accessed at runtime by reflection in Visualizer.bas and so there is no need for it at compile time which is when Android.jar is used. By the way, Android.jar holds no code, only outline definitions for the classes and methods that should be present on the phone or tablet.

The actual problem seems to be that Visualizer is not accessible by reflection if the first install of the app targets SDK 23 or higher. I've no idea why this is. Android did restrict non-SDK access by reflection from SDK 28 onwards but SDK 23 does of course predate this restriction. However it does look like you are out of luck here.

Thanks for this info.

Out of luck is : ...... :( ...... and not acceptable ;-)

My current SDK is 31 and this is the error (value of -3):

1684166467936.png
 
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
Next step:

I asked for RunTime - Permissions while running the app ..... now I get other error messages:

B4X:
** Activity (main) Create (first time) **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
vizualizer_stopdatacapture (java line: 325)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4j.object.JavaObject.RunMethod(java.lang.String, java.lang.Object[])' on a null object reference
    at b4a.example.vizualizer._stopdatacapture(vizualizer.java:325)
    at b4a.example.main._activity_pause(main.java:454)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at b4a.example.main.onPause(main.java:275)
    at android.app.Activity.performPause(Activity.java:8389)
    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1531)
    at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:5152)
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:5113)
    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:5065)
    at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:47)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2260)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4j.object.JavaObject.RunMethod(java.lang.String, java.lang.Object[])' on a null object reference
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
wavdraw_initialize (java line: 197)
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/util/ByteArrayBuffer;
    at com.stevel05.myByteArrayBuffer.Initialize(myByteArrayBuffer.java:23)
    at b4a.example.wavdraw._initialize(wavdraw.java:197)
    at b4a.example.vizualizer._drawwavforminitialize(vizualizer.java:86)
    at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:437)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:467)
    at b4a.example.main$ResumeMessage.run(main.java:313)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
Caused by: java.lang.ClassNotFoundException: org.apache.http.util.ByteArrayBuffer
    ... 17 more
wavdraw_initialize (java line: 197)
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/util/ByteArrayBuffer;
    at com.stevel05.myByteArrayBuffer.Initialize(myByteArrayBuffer.java:23)
    at b4a.example.wavdraw._initialize(wavdraw.java:197)
    at b4a.example.vizualizer._drawwavforminitialize(vizualizer.java:86)
    at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:437)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:467)
    at b4a.example.main$ResumeMessage.run(main.java:313)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
Caused by: java.lang.ClassNotFoundException: org.apache.http.util.ByteArrayBuffer
    ... 17 more

ClassNotFoundError......... :-(

I guess that this error is based on other issue in the example!
I will check this :)
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Got it! It's a permission change after SDK 22. Add these lines to Activity_Create before initialising the visualizer.
B4X:
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Log("Permission - " & Result)
    
    'Initialize the Visualizer
    Viz.Initialize(Me,AudioSession)
 
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
Got it! It's a permission change after SDK 22. Add these lines to Activity_Create before initialising the visualizer.
B4X:
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_RECORD_AUDIO)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Log("Permission - " & Result)
   
    'Initialize the Visualizer
    Viz.Initialize(Me,AudioSession)

I already added these lines - i also used them in another project!

And look at this:

This user had the same issue like me - his solution is also the same:

1684167664375.png


Thanks for your help!!

Regards
 
Upvote 0

artsoft

Active Member
Licensed User
Longtime User
Not sure what you are saying in the post above but it works for me!

I said, that I already added these RuntimePermissions lines in the example code and that another user had the same issue in 2019 as well.

...

Now it works also on my side! :)

Thanks again.

I set the title of this to SOLVED!
 
Upvote 0

amer bashar

Member
Licensed User
Next step:

I asked for RunTime - Permissions while running the app ..... now I get other error messages:

B4X:
** Activity (main) Create (first time) **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = false **
vizualizer_stopdatacapture (java line: 325)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4j.object.JavaObject.RunMethod(java.lang.String, java.lang.Object[])' on a null object reference
    at b4a.example.vizualizer._stopdatacapture(vizualizer.java:325)
    at b4a.example.main._activity_pause(main.java:454)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at b4a.example.main.onPause(main.java:275)
    at android.app.Activity.performPause(Activity.java:8389)
    at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1531)
    at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:5152)
    at android.app.ActivityThread.performPauseActivity(ActivityThread.java:5113)
    at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:5065)
    at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:47)
    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2260)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object anywheresoftware.b4j.object.JavaObject.RunMethod(java.lang.String, java.lang.Object[])' on a null object reference
sending message to waiting queue (activity_permissionresult)
running waiting messages (1)
wavdraw_initialize (java line: 197)
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/util/ByteArrayBuffer;
    at com.stevel05.myByteArrayBuffer.Initialize(myByteArrayBuffer.java:23)
    at b4a.example.wavdraw._initialize(wavdraw.java:197)
    at b4a.example.vizualizer._drawwavforminitialize(vizualizer.java:86)
    at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:437)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:467)
    at b4a.example.main$ResumeMessage.run(main.java:313)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
Caused by: java.lang.ClassNotFoundException: org.apache.http.util.ByteArrayBuffer
    ... 17 more
wavdraw_initialize (java line: 197)
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/util/ByteArrayBuffer;
    at com.stevel05.myByteArrayBuffer.Initialize(myByteArrayBuffer.java:23)
    at b4a.example.wavdraw._initialize(wavdraw.java:197)
    at b4a.example.vizualizer._drawwavforminitialize(vizualizer.java:86)
    at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:437)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:275)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:215)
    at anywheresoftware.b4a.BA$2.run(BA.java:395)
    at anywheresoftware.b4a.BA.setActivityPaused(BA.java:467)
    at b4a.example.main$ResumeMessage.run(main.java:313)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:210)
    at android.os.Looper.loop(Looper.java:299)
    at android.app.ActivityThread.main(ActivityThread.java:8168)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1037)
Caused by: java.lang.ClassNotFoundException: org.apache.http.util.ByteArrayBuffer
    ... 17 more

ClassNotFoundError......... :-(

I guess that this error is based on other issue in the example!
I will check this :)
hi, I have this same error, I have tried the RuntimePermissions solution and it didn`t worked, please how did you fix that?
 
Upvote 0
Top