sprite kit - How to stop/cancel playSoundFileNamed in Swift? -
i'm trying make button, when pushed - sound played, , when pushed again - sound stop. i'm using playsoundfilenamed: runaction: withkey:
, removeactionforkey
, doesn't work.
the other question have if there way not stop sound, pause (so start same part paused, not beginning). i've seen similar topic @ stack overflow, yet didn't find answer on question yet. thank in advance.
import spritekit import avfoundation var firesoundon = false private var backgroundmusicplayer: avaudioplayer! class gamescene2: skscene { override func didmovetoview(view: skview) { setupscenery() } private func setupscenery() { //background let background = skspritenode(imagenamed: backgroundimage, normalmapped: true) addchild(background) //fire button var firepath = uibezierpath() firepath.movetopoint(cgpointmake(0, 0)) firepath.addcurvetopoint(cgpointmake(115, 215), controlpoint1: cgpointmake(5, 170), controlpoint2: cgpointmake(90, 190)) ....
//^i'm cutting down lot of path here - button drawn , it's pushable/workable.
let firenode = skshapenode(path: firepath.cgpath, centered: false) firenode.alpha = 0.2 firenode.position = cgpointmake(size.width/2, 0) firenode.name = "firenode" self.addchild(firenode) }
//so, main bummer me:
func fireturnonoff () { if firesoundon == false { var playguitar = skaction.playsoundfilenamed("guitar.wav", waitforcompletion: true) runaction(playguitar, withkey: "soundsison") firesoundon = true println("firesoundon == \(firesoundon)") } else if firesoundon == true { removeactionforkey("soundsison") println("removed") firesoundon = false println("firesoundon == \(firesoundon)") } } override func touchesended(touches: set<nsobject>, withevent event: uievent) { touch: anyobject in touches { let touch = touches.first as? uitouch let touchlocation = touch!.locationinnode(self) let node: sknode = nodeatpoint(touchlocation) if node.name == "firenode" { fireturnonoff() } } }
use avaudioplayer. allows start, stop, pause, control volume, number of loops , other features.