audio - Android - Playing multiple sounds sequencially -
i need run many small sounds while activity running. files plays every fixed time interval (ex. 5 seconds) files played in sequence when 1 finishes next starts (ex. sound1, sound2, sound3) when screen touched.
total sounds around 35 short mp3 files (max 3 seconds).
what best way implement this?
thanks
soundpool commonly used play multiple short sounds. load sounds in oncreate(), storing positions in hashmap.
creating soundpool
public static final int sound_1 = 1; public static final int sound_2 = 2; soundpool msoundpool; hashmap<integer, integer> mhashmap; @override public void oncreate(bundle savedinstancestate){ msoundpool = new soundpool(2, audiomanager.stream_music, 100); msoundmap = new hashmap<integer, integer>(); if(msoundpool != null){ msoundmap.put(sound_1, msoundpool.load(this, r.raw.sound1, 1)); msoundmap.put(sound_2, msoundpool.load(this, r.raw.sound2, 1)); } }
then when need play sound, simple call playsound() constant value of sound.
/* *call function code sound want e.g. playsound(sound_1); */ public void playsound(int sound) { audiomanager mgr = (audiomanager)mcontext.getsystemservice(context.audio_service); float streamvolumecurrent = mgr.getstreamvolume(audiomanager.stream_music); float streamvolumemax = mgr.getstreammaxvolume(audiomanager.stream_music); float volume = streamvolumecurrent / streamvolumemax; if(msoundpool != null){ msoundpool.play(msoundmap.get(sound), volume, volume, 1, 0, 1.0f); } }
Comments
Post a Comment