ios - Swift - Have an audio repeat, but it plays in intervals? -


in swift, play song, plays @ viewdidload. 16 seconds long, , plays 16 seconds. want repeat forever. made nstimer every 16 seconds, plays song. but, plays 16 seconds when app loads, stops 16 seconds, plays, etc.

the line println("just doing dandy debugging here.") does print every 16 seconds.

how fixed?

code:

 //these var's created on top of file.  var soundtimer: nstimer = nstimer()     var audioplayer2 = avaudioplayer()  var soundtwo = nsurl(fileurlwithpath: nsbundle.mainbundle().pathforresource("sound", oftype: "wav"))  //this stuff in viewdidload function.  audioplayer2 = avaudioplayer(contentsofurl: soundtwo, error: nil)         audioplayer2.preparetoplay()         audioplayer2.play()  soundtimer = nstimer.scheduledtimerwithtimeinterval(16, target: self, selector: selector("soundtimerplayed"), userinfo: nil, repeats: true)     func soundtimerplayed() {         println("just doing dandy debugging here.")         audioplayer2.stop()         audioplayer2.preparetoplay()         audioplayer2.play() } 

just easy, official way, instead. the documentation of numberofloops property:

set negative integer value loop sound indefinitely until call stop method.

but actual problem because you're stopping playback little early:

the stop method not reset value of currenttime property 0. in other words, if call stop during playback , call play, playback resumes @ point left off.

what's happening you're stopping sound playback little before actual end of sample—your timer set short time. first time timer triggered, "play" playing tiny amount of quiet or silent sound that's left @ end, , stopping. next time, currenttime has been reset because sample has reached end of playback, on next timer interval, after 16 seconds, playback starts beginning. , repeat.

as jack observes in comment, why there's delegate callback give kick when playback has finished, i'd still set numberofloops , not bother complicated stuff, if need sound loop indefinitely.

(if desperately want repeat on exact timer event, set currenttime 0 before play again.)


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -