简体   繁体   English

Android动态壁纸中的AudioRecord

[英]AudioRecord in Android live wallpaper

I am working on a live wallpaper that uses a AudioRecord object to listen to the phone's mic. 我正在研究一种动态墙纸,该墙纸使用AudioRecord对象来收听手机的麦克风。 This works fine until I start using the wallpaper and then also view the preview wallpaper in the wallpaper selection menu. 直到我开始使用墙纸,然后再在墙纸选择菜单中查看预览墙纸时,此方法才能正常工作。 When this happens, I start having problems starting and stopping the AudioRecord object. 发生这种情况时,我开始在启动和停止AudioRecord对象时遇到问题。 Currently this is handled by onVisibilityChanged: 当前,这是由onVisibilityChanged处理的:

    @Override
    public void onVisibilityChanged(boolean visible) {
        if (this.visible == visible)
            return;

        this.visible = visible;
        if (visible) {
            recorder.startRecording();
            handler.post(drawRunner);
        } else {
            recorder.stop();
            handler.removeCallbacks(drawRunner);
        }
    }

Is there some solution to this? 有解决办法吗? I have tried several things, but nothing has worked out. 我尝试了几件事,但没有任何结果。 This solution does (rarely) work, and as far as I can tell this is because the two instances of the wallpaper engine update their visibility in an unpredictable order. 该解决方案确实(很少)有效,据我所知,这是因为墙纸引擎的两个实例以不可预测的顺序更新了它们的可见性。 Sometimes the main wallpaper sets itself to not be visible before the preview sets itself to be visible, and everything is fine. 有时主墙纸在预览将自身设置为可见之前将其自身设置为不可见,并且一切都很好。 Sometimes this order is reversed and everything breaks. 有时,此顺序被颠倒,一切都中断了。

So, I found an answer and it was stupidly simple. 因此,我找到了答案,这很简单。 I swear I tried this already and it didn't work, but everything is working fine now. 我发誓我已经尝试过了,但是没有用,但是现在一切正常。 All I had to do was move the AudioRecord object outside of the engine class and into the service class. 我要做的就是将AudioRecord对象移到引擎类之外并移到服务类中。 I then create the AudioRecord object in onCreateEngine only if it isn't already instantiated. 然后,仅在尚未实例化的情况下,才在onCreateEngine中创建AudioRecord对象。 That way, all engine instances use the same AudioRecord object. 这样,所有引擎实例都使用相同的AudioRecord对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM