• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Visualizer initCheck failed -3 (ERROR_NO_INIT)

apech

Lurker
I'm trying to create a simple audio visualizer app but keep getting this error while trying to instantiate the Visualizer class. The documentation only states that this error is due to bad object initialization. Does anyone have more information about what causes this error?

I've already made sure to have the RECORD_AUDIO and MODIFY_AUDIO_SETTINGS permissions granted before I instantiate the Visualizer. The pseudocode below shows how I'm instantiating it:
Code:
Visualizer mVizualizer;

protected void onCreate() {

    if (permissionsGranted) {
        initVisualizer();
    } else {
        requestPermissions();
    }
}

private void initVisualizer() {

    mVisualizer = new Visualizer(0); // audioSession = 0 to access output audio mix

    // setup visualizer...

}

It's my understanding that using the output audio mix requires the MODIFY_AUDIO_SETTINGS permission in addition to RECORD_AUDIO, but I already have both of these so I don't know what I'm missing.
 
According to the reference API documentation for Visualizer, ERROR_NO_INIT is due to 'Operation failed due to bad object initialization.' Not much help.
I presume you got that error in your Logcat view? Does it give you any more error messages? Do you get a stack trace?
The other thing you could try is setting a breakpoint in the code, stepping through to establish where the error is coming from, and stepping into the Visualizer code to see what's going on.
 
This error code is returned by the method


int setDataCaptureListener
()

Can you please show the part of your code which is calling this method?
 
I've set a breakpoint and stepped through the code and it's definitely not the setDataCaptureListener function. If I step into
Code:
mVisualizer = new Visualizer(0);
then I can see the native_setup function (line 9) returning the -3 error code. I can't step into this function though to see what's going on.

Code:
public Visualizer(int audioSession)
throws UnsupportedOperationException, RuntimeException {
    int[] id = new int[1];

    synchronized (mStateLock) {
        mState = STATE_UNINITIALIZED;
        // native initialization
        int result = native_setup(new WeakReference<Visualizer>(this), audioSession, id,
                ActivityThread.currentOpPackageName());
        if (result != SUCCESS && result != ALREADY_EXISTS) {
            Log.e(TAG, "Error code "+result+" when initializing Visualizer.");
            switch (result) {
            case ERROR_INVALID_OPERATION:
                throw (new UnsupportedOperationException("Effect library not loaded"));
            default:
                throw (new RuntimeException("Cannot initialize Visualizer engine, error: "
                        +result));
            }
        }
        mId = id[0];
        if (native_getEnabled()) {
            mState = STATE_ENABLED;
        } else {
            mState = STATE_INITIALIZED;
        }
    }
}

Here is the stack trace from the exception:

02-16 09:11:44.615 16933-16933/com.alexpech.visualisertest E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
02-16 09:11:44.615 16933-16933/com.alexpech.visualisertest E/visualizers-JNI: Visualizer initCheck failed -3
02-16 09:11:44.615 16933-16933/com.alexpech.visualisertest E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
02-16 09:11:44.617 16933-16933/com.alexpech.visualisertest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alexpech.visualisertest, PID: 16933
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.alexpech.visualisertest/com.alexpech.visualisertest.VisualiserActivity}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2484)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -3
at android.media.audiofx.Visualizer.<init>(Visualizer.java:218)
at com.alexpech.visualisertest.VisualiserActivity.setupVisualiserFxAndUI(VisualiserActivity.java:129)
at com.alexpech.visualisertest.VisualiserActivity.initAudio(VisualiserActivity.java:123)
at com.alexpech.visualisertest.VisualiserActivity.onCreate(VisualiserActivity.java:65)
at android.app.Activity.performCreate(Activity.java:6248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2544)
at android.app.ActivityThread.access$900(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5845)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
 
One more thing I've discovered - passing a non-zero argument to the Visualizer constructor doesn't cause an error. However it doesn't give me the output audio mix in this case.
 
The Visualizer class is very poorly documented but it seems that accessing the output audio using audioSession = 0 might be deprecated. It's not mentioned in the Visualizer class but it is in the AudioEffect class. Is anyone able to verify that this is the case?

Also, any suggestions for workarounds would be much appreciated! If I can't get the Visualizer class to behave I think I'll have to look into added a MediaPlayer and accessing the device's music through the app.
 
I know this is late to the thread, but I hope this can be helpful for anyone that has this issue in the future. I had this same issue with the -3 error, and figured out since RECORD_AUDIO is a dangerous (runtime) permission, I had to explicitly request access from the user. Having the RECORD_AUDIO permission in the manifest is not enough for runtime permissions, you have to explicitly ask the user for access. After being granted access by the user, I was able to use the VIsualizer without any issues.
 
Back
Top Bottom