android - Random music player when win in the game -
i make small game , there music when finish. have 1 sound , have 3 or 4 play randomly, hear different music each time finish. have soundmanager.java
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
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 put
private void win(){ soundmanager.playloopedsound(soundmanager.sound_winner);
thanks in advance advice.
i trying 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); }
this method works form me playraw(_this, r.raw.fail)
, should work you.
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(); }
the lines below let make settings allow turn off/on sounds. can delete it.
prefs = preferencemanager.getdefaultsharedpreferences(_this); if (prefs.getboolean(global.soundinmatch, true) == false) { return; }
in case, can turn on looping flag
mmediaplayer.setlooping(true);
Comments
Post a Comment