2016-06-16 2 views
0

작은 게임을 만들고 끝나면 음악이 있습니다. 이제는 하나의 사운드가 있고 3이나 4가 무작위로 재생되기를 원합니다. 따라서 끝날 때마다 다른 음악을 듣게됩니다. 나는 게임 활동에서게임에서 승리 할 때 무작위 음악 플레이어

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; 

또한

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

내가 어떤 조언을 사전에

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

감사를 넣어 SoundManager.java 있습니다.

내가 이런 일을하려고했지만 성공하지

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

답변

0

으로이 방법 작품은 내가 playRaw(_this, R.raw.fail), 그것은 또한 당신을 위해 일한다 형성한다.

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

아래 줄은 소리를 끄거나 켤 수있는 설정을 허용합니다. 삭제할 수 있습니다. 귀하의 경우에는

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

, 당신은 또한 플래그

mMediaPlayer.setLooping(true); 
에게 반복 켤 수 있습니다