无法在循环中播放声音 - android

时间:2013-12-26 15:45:05

标签: android audio

当我不在循环中播放声音时,它工作正常,但是当我尝试循环播放或不止一次播放时它根本不播放。

我的声音课程:

public class GameSounds {
    private  SoundPool soundPool;
    private  HashMap soundPoolHashMap;
    Context mContext;

    public GameSounds() {
        soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
        soundPoolHashMap = new HashMap();
    }

    /**
     * 
     * @param index is the ID *we* choose for this sound
     * @param soundID is the id of the sound file in our resurce
     * @param context - our resurce is in this context
     */
    public void addSound(int index, int soundID, Context context) {
        //we call the "load" function in order to convert the sound in the soundID to raw
        //and to insert the ID that return from "load" to soundPoolID
        int soundPoolID = soundPool.load(context, soundID, 1);

        mContext = context;
        //insert new value to the hash
        soundPoolHashMap.put(index, soundPoolID);
    }

    /**
     * 
     * @param index - the sound code we want to sound
     * @param loop - whether to sound this sound in infintly loop
     */
    public void play(int index, boolean loop) {
        SharedPreferences preferences=PreferenceManager.getDefaultSharedPreferences(mContext);

        if(preferences.getBoolean("isMuted", false)){
            if (!loop)
                //the "1" is for highest volum
                soundPool.play((Integer) soundPoolHashMap.get(index), 1, 1, 1, 0, 1f);
            else
                soundPool.play((Integer) soundPoolHashMap.get(index), 1, 1, 1, -1, 1f); //even when I write 10 instead of -1 , it is not do anything...
        }
    }
    public void stop(int index) {
        soundPool.stop((Integer) soundPoolHashMap.get(index));
    }

    public void release() {
        soundPool.release();
    }
}

现在,这就是我播放声音的方式:

    gameSounds.play(this.gameLevel.music,false); //work good - one time
    gameSounds.play(this.gameLevel.music,true); //not work at all

可能是什么原因?谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个我的课 - 它适用于我:

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.util.SparseArray;

public class SoundManager {

    public static int SND_MENU_BTN      = 0;
    public static int SND_WIN           = 1;
    public static int SND_LOOSE         = 2;
    public static int SND_DRAW          = 3;
    public static int SND_TICK1         = 4;
    public static int SND_TICK2         = 5;
    public static int SND_OUT_OF_TIME   = 6;
    public static int SND_HISCORE       = 7;

    public static boolean isSoundTurnedOff;

    private static SoundManager mSoundManager;

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

    public static final int maxSounds = 3; //an object to change if need

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

        //actually the following preloads all the sounds
        mSoundPoolMap = new SparseArray<Integer>(); 
        mSoundPoolMap.put(SND_MENU_BTN, mSoundPool.load(mContext, R.raw.menubutton, 1));
        mSoundPoolMap.put(SND_WIN, mSoundPool.load(mContext, R.raw.win, 1));
        mSoundPoolMap.put(SND_LOOSE, mSoundPool.load(mContext, R.raw.lose, 1));
        mSoundPoolMap.put(SND_DRAW, mSoundPool.load(mContext, R.raw.hangmanbodydrawing, 1));
        mSoundPoolMap.put(SND_TICK1, mSoundPool.load(mContext, R.raw.tick_0, 1));
        mSoundPoolMap.put(SND_TICK2, mSoundPool.load(mContext, R.raw.tick_1, 1));
        mSoundPoolMap.put(SND_OUT_OF_TIME, mSoundPool.load(mContext, R.raw.out_of_time, 1));
        mSoundPoolMap.put(SND_HISCORE, mSoundPool.load(mContext, R.raw.personal_highscore, 1));

    } 


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

显然要播放一些定义的声音,你需要写一些像(如果在活动中):

SoundManager.getInstance(this).playSound(SoundManager.SND_TICK1);

然而,在我的项目中,我只需要调用SoundManager.getInstance(this)一次来启动它,然后在每个活动中我都会保存一个指向SoundManager的链接,以缩短用法,如:

soundManager.playSound(SoundManager.SND_TICK1);