单击几次后,SoundPool无法播放声音

编程入门 行业动态 更新时间:2024-10-28 10:33:53
本文介绍了单击几次后,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无法播放声音

本文发布于:2023-07-11 09:36:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1093605.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:几次   单击   无法播放   声音   SoundPool

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!