android.media.audiofx.Visualizer throwing exception every other time
Asked Answered
R

1

9

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.

Ranunculaceous answered 10/2, 2012 at 3:15 Comment(0)
G
26

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()

Greywacke answered 14/2, 2012 at 15:24 Comment(4)
That worked! Thank you so much. It says I can't award the bounty for another 6 hours but I will once I can.Ranunculaceous
I'm glad to hear. I have never worked with Visualizer before, and I hope my method of finding the issue will serve you in other times of need.Greywacke
This helped me to fix a similar error I was having with screen orientation! Great job thanks.Brute
Been looking for this answer a couple of days now, THANK YOU!Sharpset

© 2022 - 2024 — McMap. All rights reserved.