首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Soundpool问题

Android Soundpool问题
EN

Stack Overflow用户
提问于 2014-03-22 10:58:05
回答 2查看 327关注 0票数 0

最近我一直在开发一款android游戏,但在添加音乐和其他声音时遇到了问题。我读到Mediaplayers最适合播放背景音乐,效果很好,我还读到Soundpool最适合播放短小的音乐片段,比如音效。但是,我无法让Soundpool工作,似乎Soundpool甚至没有加载。

我做了更多的研究,发现了很多关于Soundpool缺乏支持的噩梦故事,所以我应该只使用mediaplayer来处理所有的音频吗?这是有效的吗?

代码语言:javascript
复制
//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
EN

回答 2

Stack Overflow用户

发布于 2014-03-24 19:54:21

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-03-24 14:19:46

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

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22572879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档