2016-06-16 1 views
0

Je fais du petit jeu et il y a de la musique quand tu finis. Maintenant, j'ai un son et j'aimerais avoir 3 ou 4 qui jouent aléatoirement, donc vous entendez de la musique différente chaque fois que vous finissez. Je SoundManager.java oùLecteur de musique aléatoire quand gagner dans le jeu

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; 

et aussi

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

Dans l'activité de jeu, je mis

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

Merci à l'avance pour tout conseil.

je tente de faire quelque chose comme ça, mais sans succès

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

Répondre

0

Cette méthode fonctionne me font playRaw(_this, R.raw.fail), il faut aussi travailler pour vous.

private static void playRaw(Context _this, int resource) { 
    PREFS = PreferenceManager.getDefaultSharedPreferences(_this); 
    if (PREFS.getBoolean("SOUND_IN_GAME", true) == false) { 
     return; 
    } 
    MediaPlayer mMediaPlayer = MediaPlayer.create(_this, resource); 
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
    mMediaPlayer.setLooping(false); 
    mMediaPlayer.start(); 
} 

Les lignes ci-dessous vous permettent d'effectuer des réglages qui vous permettent d'activer ou de désactiver les sons. Vous pouvez le supprimer.

PREFS = PreferenceManager.getDefaultSharedPreferences(_this); 
if (PREFS.getBoolean(Global.soundInMatch, true) == false) { 
    return; 
} 

Dans votre cas, vous pouvez également activer une boucle drapeau

mMediaPlayer.setLooping(true);