0

我正在做一个警报系统。警报工作正常,但声音不起作用..我正在做一个小测试,但我不明白问题出在哪里..在警报活动中,我有以下代码:

        setVolumeControlStream(AudioManager.STREAM_MUSIC);
    soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
    try {
        AssetManager assetManager = getAssets();
        AssetFileDescriptor descriptor = assetManager.openFd("sound.ogg");
        mySoundId = soundPool.load(descriptor, 1);
    } catch (IOException e) {
        Log.d(TAG,"We've problem's!!, "
                + e.getMessage());
    }   

    soundPool.play(mySoundId, 1, 1, 0, 0, 1);

没有错误..但音乐不起作用..相反,如果我在底部工作中编程 PLAY!任何人都可以帮助我吗?

4

1 回答 1

1

我刚刚尝试了您的代码并发现了问题。这是 SoundPool 中的常见问题。在播放之前,您需要等待声音完成加载。这就是为什么soundPool.play()返回零。声音还没准备好。

在 API 8 中,setOnLoadCompleteListener()引入了解决问题的方法:

soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
       //play the sound.
    }
});

但是,如果您的目标是较低的 API,您无法确定您的声音是否已准备好播放。在这种情况下,我建议您使用MediaPlayer

于 2012-09-03T06:45:28.903 回答