【问题标题】:Android Soundpool IssueAndroid Soundpool 问题
【发布时间】:2014-04-29 15:41:31
【问题描述】:

最近我一直在开发一款安卓游戏,但在添加音乐和其他声音时遇到了问题。我读到 Mediaplayers 最适合背景音乐,效果很好,我读到 Soundpool 最适合像音效这样的短音乐剪辑。但是,我无法让 Soundpool 工作,似乎 Soundpool 甚至没有加载。

我做了更多的研究,发现了很多关于 Soundpool 缺乏支持的噩梦故事,所以我应该只使用媒体播放器来播放所有音频吗?这效率高吗?

//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


    【解决方案1】:
    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);
        }
    });
    

    因为你加载的变量总是假的,你应该在 onLoadComplete 中做这些事情。

    【讨论】:

      【解决方案2】:

      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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-31
        • 1970-01-01
        • 2013-08-12
        相关资源
        最近更新 更多