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.

Foggzie
  • 9,691
  • 1
  • 31
  • 48

1 Answers1

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

MByD
  • 135,866
  • 28
  • 264
  • 277
  • That worked! Thank you so much. It says I can't award the bounty for another 6 hours but I will once I can. – Foggzie Feb 14 '12 at 17:18
  • 1
    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. – MByD Feb 14 '12 at 17:19
  • This helped me to fix a similar error I was having with screen orientation! Great job thanks. – kabuto178 Jun 01 '13 at 19:11
  • Been looking for this answer a couple of days now, THANK YOU! – Marcus Oct 31 '14 at 09:30