i write android game 1 unit has 3 audio stages:
- get hit
- attack
- dead
so added unit object followed code:
mediaplayer mmediaplayergethit = null; mediaplayer mmediaplayerdead = null; mediaplayer mmediaplayerattack = null;
in constructor
:
mmediaplayerattack=mediaplayer.create(context,r.raw.fly_monster_attack1); mmediaplayerdead=mediaplayer.create(context,r.raw.fly_monster_death1); mmediaplayergethit=mediaplayer.create(context,r.raw.fly_monster_gethit1); mmediaplayerattack.setonpreparedlistener(this); mmediaplayerdead.setonpreparedlistener(this); mmediaplayergethit.setonpreparedlistener(this); ....
now it's seem messy,
hmm, 3 player objects, 1 per unit state.
i'm sure wrong way , need change each time mediaplayer
source regarding unit state,
but here comes performance problem:
lets mediaplayer
has took sources attack
.
after, switch get hit
, after once more attack
.
maybe need save instance of mediaplayer
attack
somehow , reuse it?
i don't see way hundreds times switch poor mediaplayer
.
any ideas?
thank you,
for short sound bites those, should using soundpool instead of mediaplayer, although uses mp decode audio..it's designed low latency streams, , allows multiple streams @ same time.
in addition low-latency playback, soundpool can manage number of audio streams being rendered @ once. when soundpool object constructed, maxstreams parameter sets maximum number of streams can played @ time single soundpool. soundpool tracks number of active streams. if maximum number of streams exceeded, soundpool automatically stop playing stream based first on priority , age within priority. limiting maximum number of streams helps cap cpu loading , reducing likelihood audio mixing impact visuals or ui performance.
so, game multiple sounds need rendered @ once, soundpool may ideal.
Comments
Post a Comment