2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Bel
  • 302
  • 4
  • 16

3 Answers3

1

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.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
  • I need get full audio output. – Bel Jan 30 '13 at 07:28
  • private void setupVisualizer() { astroVisualizer.setEnabled( Log.v("ABS","setupVisualizer" + astroVisualizer.getEnabled()); //log astroVisualizer.setDataCaptureListener(this,250,false,true); astroVisualizer.setEnabled(true); } – Bel Jan 30 '13 at 08:07
0

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.

Dominic Cerisano
  • 3,522
  • 1
  • 31
  • 44
  • Seems harsh. Has anything changed since April? I'd love to make use of this class, but don't want to hop on board if support is going nowhere – scone Jan 13 '16 at 20:17
  • It is worse with the continued fragmentation of Android. Some device/os combinations just wont allow access to audio channels at all (eg chromebooks). When things like access to the microphone and DRM are considered, it is not at all surprising. Best bet would be to access the streams directly via the linux /dev buffers with C/C++ NDK (which is how visualizer and equalizer do it, but are subject to java security sandbox.) – Dominic Cerisano Jan 15 '16 at 14:59
0

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.

gautam90
  • 58
  • 7