avaudioplayer - How to have multiple audio streams play overlapping in iOS? -
i defining 3 area of screen 'hot-spots' if there touch detected, plays zone-specific sound. works great, except if click on zone 1 , zone 2, zone 2 sounds cuts off zone 1. want sound of zone play until completed. so, if click zone 1 , zone 2, zone 1's sound keep playing, , zone 2 play on zone 1.
here code have (which works fine, doesn't allow audio overlaps):
if(point.y < 316 && point.y > 76 && point.x < 220 && point.x > 200) { nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/sound1.mp3", [[nsbundle mainbundle] resourcepath]]]; nserror *error; audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:&error]; audioplayer.numberofloops = 0; if (audioplayer == nil) nslog([error description]); else [audioplayer play]; } if(point.y < 316 && point.y > 76 && point.x < 260 && point.x > 240) { nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/sound2.mp3", [[nsbundle mainbundle] resourcepath]]]; nserror *error; audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:&error]; audioplayer.numberofloops = 0; if (audioplayer == nil) nslog([error description]); else [audioplayer play]; } if(point.y < 316 && point.y > 76 && point.x < 300 && point.x > 280) { nsurl *url = [nsurl fileurlwithpath:[nsstring stringwithformat:@"%@/sound3.mp3", [[nsbundle mainbundle] resourcepath]]]; nserror *error; audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:url error:&error]; audioplayer.numberofloops = 0; if (audioplayer == nil) nslog([error description]); else [audioplayer play]; } can please me figure out how make these sounds overlap? thanks!
it looks you're reassigning audioplayer each case. if audioplayer playing 1 sound , reassign play other sound, original audioplayer destroyed , stop playing.
the way i've solved problem in past set multiple avplayer objects, like:
avplayer *_player[max_channels]; nsuinteger _currentchannel; then play:
_player[_currentchannel] = [[avaudioplayer alloc] initwithcontentsofurl:url error:&error]; _currentchannel++; if (_currentchannel == max_channels) { _currentchannel = 0; } then don't infinite number of overlapping sound effects, can predefine "reasonable" number max_channels , have many overlapping effects before next sound stops first sound.
Comments
Post a Comment