android.media.audiofx.Visualizer 每隔一段时间就会抛出异常
我正在为 Android 2.3.3 制作动态壁纸,它使用了 Visualizer 类。我已经有了一个可以独立运行的 Visualizer 程序的工作版本,但是当我将代码放入动态壁纸服务中时,我的问题就开始了。以下代码是错误存在的地方:
// 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);
}
这是奇怪的部分,当我查看动态壁纸列表时,我会点击它来查看预览,它工作正常。在没有将其设置为活动壁纸的情况下,我点击后退按钮,然后再次选择它,然后它崩溃了。我可以重复这个过程,它只会每隔一次崩溃一次,而其他时候则有效。如果我选择将其设置为活动壁纸,它每次都会崩溃。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
寻找 在源代码中,如果状态不是
STATE_INITIALIZED
,则似乎会抛出IllegalStateException
。由于构造函数将状态设置为
STATE_ENABLED
或STATE_INITIALIZED
,这意味着您收到异常时的状态是STATE_ENABLED
(唯一的选项)。在 setCaptureSize() 的文档中,他们提到当状态为 STATE_ENABLED 时不应调用此方法,因此我认为您需要调用 setEnabled(false) 在调用
setCaptureSize()
之前在 Visualizer 对象上Looking at the source, it seems like
IllegalStateException
is thrown if the state is notSTATE_INITIALIZED
.Since the constructor sets the state to
STATE_ENABLED
orSTATE_INITIALIZED
, it means that the state when you get the exception isSTATE_ENABLED
(the only option).In the documentation of
setCaptureSize()
they mention that you should not call this method while the state isSTATE_ENABLED
, so I think you need to callsetEnabled(false)
on the Visualizer object before callingsetCaptureSize()