Android SoundPool sample 1 not ready错误解决方法

来源:互联网 发布:手机五彩闪光灯软件 编辑:程序博客网 时间:2024/05/22 09:38
Android SoundPool sample 1 not ready错误解决方法

DDMS报的错是sample not ready的问题,也就是说是在load加载音乐文件出错,导致在play播放音乐时显示not ready;
在SoundPool中有setOnLoadCompleteListener方法用来判断音乐加载是否完成,因此解决方法如下:

一、调用setOnLoadCompleteListener方法来确保音乐加载完成,注意需要SoundPool.OnLoadCompleteListener listener

需要实现SoundPool.OnLoadCompleteListener接口。

soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener(){
            @Override
            public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
                soundPool.play(soundPoolMap.get(soundId), // 声音资源id
volume, // 左声道音量
volume, // 右声道音量
1, // 优先级
loop, // 循环次数 -1带表永远循环
0.5f // 回放速度0.5f~2.0f之间
);
            }});


二、可直接在load后面加sleep(1000),具体时间根据加载的文件的多少大小而定,给程序足够的时间去加载初始化音频文件。

// 将加载的声音资源id放进此Map
soundPoolMap.put(1, soundPool.load(this, R.raw.gamestart, 1));
try {
Thread.sleep(1000);// 给予初始化音乐文件足够时间
} catch (InterruptedException e) {
e.printStackTrace();
}


0 0
原创粉丝点击