Android Soundpool Issue

问题描述 投票:0回答:2

最近,我一直在从事Android游戏,但是在添加音乐和其他声音时遇到了问题。我读到Mediaplayers是背景音乐的最佳选择,效果很好,并且我读到Soundpool对于诸如音效之类的短音乐片段是最佳的。但是,我无法使Soundpool正常工作,似乎Soundpool甚至没有加载。

我做了更多的研究,发现了很多关于Soundpool缺乏支持的噩梦,所以我应该只使用Media Player播放所有音频吗?这样有效吗?

//My Code
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int mySoundId, int status) {
            loaded = true;
        }
    });

    if(loaded==true){
     soundId = soundPool.load(this, R.raw.sound1, 1);
     soundPool.play(soundId, 1, 1, 1, 0, 1f);}
    else{
         System.exit(0);
     }
            //Forces the program to exit everytime
android audio soundpool
2个回答
0
投票
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int mySoundId, int status) {
        loaded = true;
        soundId = soundPool.load(this, R.raw.sound1, 1);
        soundPool.play(soundId, 1, 1, 1, 0, 1f);
    }
});

因为您的已加载变量始终为false,所以应在onLoadComplete中执行操作。


0
投票

[SoundPool#load方法是异步的,应该在调用onLoadComplete之后播放声音]

soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
    @Override
    public void onLoadComplete(SoundPool soundPool, int mySoundId, int status) {        
        soundPool.play(mySoundId, 1, 1, 1, 0, 1f);
    }
});

soundPool.load(this, R.raw.sound1, 1);
© www.soinside.com 2019 - 2024. All rights reserved.