mercredi 15 juin 2016

Random music player when win in the game

I make small game and there is music when you finish. Now I have one sound and would like to have 3 or 4 which play randomly, so you hear different music each time you finish. I have SoundManager.java where

public static final int SOUND_WINNER = 4;
public static final int SOUND_LOSER = 5;
public static final int SOUND_WINNER2 = 6;
public static final int SOUND_WINNER3 = 7;

and also

    public static void initSounds(Context theContext) { 
    mContext = theContext;
    mSoundPool = new SoundPool(10, AudioManager.STREAM_SYSTEM, 0); 
    mSoundPoolMap = new HashMap<Integer, Integer>(); 
    loopedSoundMap = new HashMap<Integer, Integer>();
    mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);      

    addSound(SOUND_WINNER, R.raw.win);
    addSound(SOUND_LOSER, R.raw.lose);
    addSound(SOUND_WINNER2, R.raw.win2);
    addSound(SOUND_WINNER2, R.raw.win3);

In game activity I put

    private void win(){ 
    SoundManager.playLoopedSound(SoundManager.SOUND_WINNER);

Thanks in advance for any advice.

I was trying to do something like this but with no success

private void win(){ 
    Random random=new Random();
    int r = random.nextInt(4);
    if(r==0){
    SoundManager.playLoopedSound(SoundManager.SOUND_WINNER);
    } 
    if(r==1){
    SoundManager.playLoopedSound(SoundManager.SOUND_WINNER2);
    } 




Aucun commentaire:

Enregistrer un commentaire