简体   繁体   中英

Android Cannot initialize Visualizer engine, error: -4

I have an error in:

public class VisualizerCapture extends Activity implements Visualizer.OnDataCaptureListener {
private Visualizer mVisualizer = new Visualizer(0); // error is here!!!

@Override
public void onCreate(Bundle savedInstanceState){
 super.onCreate(savedInstanceState);

    setupVisualizer();
}

This is the thrown error:

  java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{bla bla}: java.lang.RuntimeException: Cannot initialize Visualizer engine, error: -4

My manifest:

    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>    
    <uses-permission android:name="android.permission.INTERNET"/>         

Setup method:

private void setupVisualizer() {
    Visualizer.setEnabled(false);
    Visualizer.setCaptureSize(1);      //test
    Visualizer.setDataCaptureListener(this,250,false,true);
    //Visualizer.setScalingMode(visualizer.SCALING_MODE_AS_PLAYED);
    Visualizer.setEnabled(true);
            Log.v("ABS","setupVisualizer" + Visualizer.getEnabled());    //log

}

Why I got this error? Seems like I have sated all permissions?

Be patient please, I am newbie in development. How can I fix this?

After reading the documentation:

public Visualizer (int audioSession)

Added in API level 9
Class constructor.

Parameters
audioSession    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.

Are you sure you want 0 ?

Update

Looking further into the error:

public static final int ERROR_BAD_VALUE

Added in API level 9
Operation failed due to bad parameter value.

Constant Value: -4 (0xfffffffc)

That is the error your're getting, it is likely you have something bad in the configuration of the Visualizer, double check the content in your setupVisualizer method.

You cannot get Visualizer to run on all platforms. It is one of the least tested objects in Android and should not have been released imho. You absolutely need to catch exceptions wherever you touch it.

Visualizer is most likely one of the most complex and undocumented classes you will ever use. It has DSP and FFT weirdness combined with arcane error handling.

Eg. You should always also instantiate an Equalizer when using Visualizer, in order to bypass volume controls.

You should never instantiate object dependencies in the declaration section of a class. This makes it difficult to catch exceptions, and also makes dependency injection difficult.

You should instantiate objects in the constructor or init method of a class so that you can catch exceptions and also support testing mocks for dependency injection.

Add permission in manifest file

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

If added then check,

Goto AppInfo for this app; navigate to permissions; ensure all permissions that this app has requested has been granted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM