【问题标题】:Android - Soundpool do not play all sounds with many apps in the background.Android - Soundpool 不会在后台播放许多应用程序的所有声音。
【发布时间】:2016-07-21 21:03:34
【问题描述】:

我正在制作一个包含大量声音的游戏,我使用 Soundpool 来播放它们。在几乎每一种声音中,我都会创建一个新线程,以便在主 UI 线程中不加载太多。像这样:

public void playSatelliteWinning(final Boolean isSound) {
    new Thread(new Runnable() {
        public void run() {
            if (isSound && !satelliteIsPlaying) {
                satelliteIsPlaying = true;
                handler.postDelayed(new Runnable() {

                    public void run() {
                        play(satelliteWinning);
                    }
                }, 100);
                satelliteIsPlaying = false;
            }
        }
    }).start();
}

但是当我在我的手机上运行许多其他应用程序时,我的游戏中不会播放许多声音。当我杀死我手机上运行的所有应用程序然后开始我的游戏时,声音没有问题。 似乎是某种缺乏记忆之类的东西。有没有人有类似的问题或解决这个问题?有任何想法吗?谢谢!

我在 Nexus 5X、Android 6.0 上运行我的游戏

【问题讨论】:

  • 由于某种原因,似乎是 Candy Crush Soda 导致了这个问题。如果我杀死 Candy Crush Soda 并在后台运行许多其他应用程序,我的声音就没有问题。

标签: android background soundpool


【解决方案1】:

默认线程池大小为 5,我认为这就是阻止您的原因。 我认为你应该创建一个 SoundManager。

public class SoundManager {

    public static int SOUNDPOOLSND_MENU_BTN         = 0;
    public static int SOUNDPOOLSND_WIN              = 1;
    public static int SOUNDPOOLSND_LOOSE            = 2;
    public static int SOUNDPOOLSND_DRAW             = 3;
    public static int SOUNDPOOLSND_TICK1            = 4;
    public static int SOUNDPOOLSND_TICK2            = 5;
    public static int SOUNDPOOLSND_OUT_OF_TIME      = 6;
    public static int SOUNDPOOLSND_HISCORE          = 7;
    public static int SOUNDPOOLSND_CORRECT_LETTER   = 8;

    public static boolean isSoundTurnedOff;

    private static SoundManager mSoundManager;

    private SoundPool mSoundPool; 
    private SparseArray <Integer> mSoundPoolMap; 
    private AudioManager  mAudioManager;

    public static final int maxSounds = 4;

    public static SoundManager getInstance(Context context)
    {
        if (mSoundManager == null){
            mSoundManager = new SoundManager(context);
        }

        return mSoundManager;
   }

    public SoundManager(Context mContext)
    {
        mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        mSoundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0);

//        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
//            public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
//               loaded = true;
//            }
//        });

        mSoundPoolMap = new SparseArray<Integer>(); 
        mSoundPoolMap.put(SOUNDPOOLSND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_WIN, mSoundPool.load(mContext, R.raw.win, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1));
        mSoundPoolMap.put(SOUNDPOOLSND_CORRECT_LETTER, mSoundPool.load(mContext, R.raw.correct_letter, 1));

        // testing simultaneous playing
        int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
        mSoundPool.play(mSoundPoolMap.get(0), streamVolume, streamVolume, 1, 20, 1f); 
        mSoundPool.play(mSoundPoolMap.get(1), streamVolume, streamVolume, 1, 2, 1f);
        mSoundPool.play(mSoundPoolMap.get(2), streamVolume, streamVolume, 1, 0, 1f);


    } 

    public void playSound(int index) { 
        if (isSoundTurnedOff)
            return;

         int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 
         mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f); 
    }

    public static void clear()
    {
        if (mSoundManager != null){
            mSoundManager.mSoundPool = null; 
            mSoundManager.mAudioManager = null;
            mSoundManager.mSoundPoolMap = null;
        }
        mSoundManager = null;
    }
}

你可以添加一个方法来播放特定的声音,比如

  public void playSound(SoundsEnum enum){
        // play the sound with that value
    }

【讨论】:

  • 好的!太好了,我会试试这个!如果它工作正常,我会接受你的回答! :) 谢谢!
  • 嗯...我用一键点击的声音试了一下。它只播放了2次声音,然后就没有声音了。 :( 我现在只在手机上运行我的游戏。后台没有应用程序。
  • 奇怪,我还是有这个问题,当我关闭我的应用程序并再次打开它时,有些声音没有播放。
  • 这里也一样。我尝试在 SoundPool 中加载 8 个声音,然后按时间间隔一一播放,它只播放 4 或 5 个。其余的 load() 返回 0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
相关资源
最近更新 更多