Android音效SoundPool问题:soundpool 1 not retry

本文介绍了解决Android开发中SoundPool加载声音文件后立即播放导致soundpool1notretry问题的方法。通过延迟播放时间,确保声音文件加载完成后再进行播放。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android音效SoundPool问题:soundpool 1 not retry


今天开发中要用到SoundPool,遇到soundpool 1 not retry无法播放声音,MediaPlay可以


后来经过一番研究,发现:

出现soundpool 1 not retry问题的代码,无法播放声音

mgr = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
        //初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.or, 1));
        soundPoolMap.put(2, soundPool.load(MainActivity.this, R.raw.sd, 1));
        volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);

//loop:循环中的循环模式(0 =没有循环,-1 =无限循环)
                soundPool.play(soundPoolMap.get(1), volume, volume, 1, 0, 1f);

问题解决:这里的问题是soundPool.load(MainActivity.this, R.raw.or, 1),即 load() 声音文件后,立马 play() 播放,系统还没有准备好声音文件,所以才出了问题

这里需要你:先在其他地方load()好了,比如在构造函数里先load()好了,在需要播放的地方再调用play(),也就是要过一段时间再调用play()

这样写就没问题

mgr = (AudioManager) MainActivity.this.getSystemService(Context.AUDIO_SERVICE);
        //初始化soundPool 对象,第一个参数是允许有多少个声音流同时播放,第2个参数是声音类型,第三个参数是声音的品质
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
        soundPoolMap = new HashMap<Integer, Integer>();
        soundPoolMap.put(1, soundPool.load(MainActivity.this, R.raw.or, 1));
        soundPoolMap.put(2, soundPool.load(MainActivity.this, R.raw.sd, 1));
        volume = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        soundPool.play(soundPoolMap.get(1), volume, volume, 1, 0, 1f);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值