单击几次后,SoundPool无法播放声音 [英] SoundPool doesn`t play sound after clicking several times

查看:134
本文介绍了单击几次后,SoundPool无法播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击按钮时,会在我的应用程序上播放声音,但持续单击一段时间后将不再播放.也许是内存问题?你能解决我的问题吗?这是通过SoundPool实现的播放声音:当我单击按钮时,我调用了play方法:而play方法在后台线程起作用

When i click button plays sound on my app, but keep clicking for a while doesn`t play anymore. Maybe it is memory issue? Could you solve me my issue? Here is implemented play sound via SoundPool: when i click button i called play method: and play method works background thread

private void play(int resId) {  
    soundPool = buildBeforeAPI21();//TODO: refactor with deprecated constructor
    soundPool.load(activity, Integer.parseInt(resId), 1);
}

public SoundPool buildBeforeAPI21() {
    if (soundPool == null) {
        soundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
        soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                if (status == 0) {
                    soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f);
                }
            }
        });
    }
    return soundPool;
}

推荐答案

不要每次点击都创建一个新的SoundPool.那是非常浪费的.改为执行此操作:

Don't create a new SoundPool with each click. That is extremely wasteful. Do this instead:

1)仅创建一次SoundPool2)将您想要播放的所有声音加载一次3)等待所有声音加载4)每次单击即可调用soundPool.play().

1) Create the SoundPool just once 2) Load it with all the sounds you want to play, just once 3) Wait for all the sounds to load 4) Just call soundPool.play() with each click.

这篇关于单击几次后,SoundPool无法播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆