ios - Sprite Kit stop playing music file and start from the beginning -
whenever present scene, want start playing music file , stop music playing last time scene presented. i'm trying this.
[self removeactionforkey:@"music"]; [self runaction:[skaction repeatactionforever:[skaction playsoundfilenamed:@"music.wav" waitforcompletion:yes]] withkey:@"music"];
should use avaudioplayer
or [skaction playsoundfilenamed:]
?
also know if there way play music file forever in scenes without starting beginning.
to play background music in scenes recommend avaudioplayer
. can write code somewhere in appdelegate
access scenes. here sample code achieve using avaudioplayer
// avaudioplayer *bgmusicloop; - (void)playbackroundmusic:(nsstring *)filename withextenstion:(nsstring *)fileextension { nsstring *filepath = [[nsbundle mainbundle] pathforresource:filename oftype:fileextension]; nserror *error; if(bgmusicloop) [bgmusicloop stop]; bgmusicloop = nil; bgmusicloop = [[avaudioplayer alloc] initwithcontentsofurl:[nsurl fileurlwithpath:filepath] error:&error]; if (error) { nslog(@"error in audioplayer: %@", [error localizeddescription]); } else { bgmusicloop.numberofloops = -1; [bgmusicloop preparetoplay]; [bgmusicloop play]; } } - (void)stopbackgroundmusic { if(bgmusicloop) [bgmusicloop stop]; bgmusicloop = nil; } - (void)pausebackgroundmusic { if(bgmusicloop && bgmusicloop.isplaying) [bgmusicloop pause]; } - (void)resumebackgroundmusic { if(bgmusicloop && !bgmusicloop.isplaying) [bgmusicloop play]; }
Comments
Post a Comment