Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android.media.audiofx.Visualizer throwing exception every other time

I'm making a Live Wallpaper for Android 2.3.3 and it used the Visualizer class. I've already got a working version of my Visualizer program working as a stand alone but when I place the code into a Live Wallpaper service, my problem begins. The following code is where the error exists:

// Called in my Engine extension's 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'm looking through the live wallpaper list, I'll tap it to view the preview and it works fine. Without setting it as the active wallpaper, I hit the back button and then select it again and it crashes. I can repeat this process and it only crashes every other time and works the other times. If I choose to set it as the active wallpaper, it crashes every time.

like image 616
Foggzie Avatar asked Feb 10 '12 03:02

Foggzie


1 Answers

Looking at the source, it seems like IllegalStateException is thrown if the state is not STATE_INITIALIZED.

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

In the documentation of setCaptureSize() they mention that you should not call this method while the state is STATE_ENABLED, so I think you need to call setEnabled(false) on the Visualizer object before calling setCaptureSize()

like image 121
MByD Avatar answered Oct 21 '22 08:10

MByD