Android.media.audiofx.Visualizer throws an exception at another time

I am making a Live Wallpaper for Android 2.3.3 and it used the Visualizer class. I already have a working version of my Visualizer program, which works as a stand-alone, but when I put the code in the Live Wallpaper service, my problem starts. The following code contains an error:

// Called in my Engine extension constructor public void setupVisualizer() { mBytes = null; mVisualizer = new Visualizer(0); // EDIT mVisualizer.setEnabled(false); // This fixes the issue // END EDIT mVisualizer.setCaptureSize( Visualizer.getCaptureSizeRange()[1]); // IllegalStateException Thrown mVisualizer.setDataCaptureListener() { public void onWaveFormDataCapture(Visualizer visualizer, byte[] bytes, int samplingRate) { updateVisualizer(bytes); } public void onFftDataCapture(Visualizer visualizer, bytes[] bytes, int samplingRate) {} }, Visualizer.getMaxCaptureRate() / 2, true, false); mVisualizer.setEnabled(true); } 

Here's the weird part, when I look at a list of live wallpapers, I click on it to preview it, and it works great. Without setting it as active wallpaper, I press the back button and then select it again and it crashes. I can repeat this process, and it only crashes every time and works at a different time. If I decide to set it as an active wallpaper, it will be reset every time.

+7
source share
1 answer

In a search in the source, it seems that IllegalStateException if the state is not STATE_INITIALIZED .

Since the constructor sets the state to STATE_ENABLED or STATE_INITIALIZED , this means that the state when you get the exception is STATE_ENABLED (the only option).

The documentation for setCaptureSize() mentions that you should not call this method while the state is STATE_ENABLED , so I think you need to call setEnabled(false) in the Visualizer object before calling setCaptureSize()

+21
source

All Articles