Android - Soundpool 不会在后台播放许多应用程序的所有声音。

标签 android background soundpool

我正在制作一款有很多声音的游戏,我使用 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 上运行我的游戏

最佳答案

默认的线程池大小是 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
    }

关于Android - Soundpool 不会在后台播放许多应用程序的所有声音。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38504778/

相关文章:

android - 在 Android Studio 上添加 Google Play 服务依赖项的最佳方式是什么?

android - 具有单一主要 Activity 的应用的应用索引

android - 如何将 AIRPLANE_MODE_ON 设置为 "True"或 ON?

android - 无法获取 TYPE_SYSTEM_OVERLAY 的触摸事件

java - 无限的游戏背景

c# - 如何从位图中删除白色背景色

android - AudioFlinger 无法创建音轨,状态 : -12; Error creating AudioTrack

android - Android Soundpool问题

android - SoundPool 不播放?

android - 更新数据库sqlite后更新listview