mardi 17 février 2015

How do I stop playing a sound effect with SpriteKit and Swift?

I'm writing a storybook app for my niece and I have a question about SpriteKit. I'm trying to set it so that there are different types of audio that play.



  1. Background music that loops (AVFoundation)

  2. Narration that plays when on a new page, or when you press the narrate button to replay the narration (SKAction)


My problem is that the narration will play on top of each other if the user changes the page, or if the user plays the replay narration button. So it ends up sounding like two people talking over each other.


How can I stop all narrations that are playing when a new narration is triggered?


I can't find any relevant help on the internet. I've seen some posts saying us AVFoundation, but from my understanding (albeit limited) that seems more for the background music and can only have one track playing.


Am I misinterpreting the documentation? Can someone help me answer this problem?



import SpriteKit


import AVFoundation


class Page1: SKScene {



// MARK: Touch handling

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch: AnyObject in touches {
let location = touch.locationInNode(self)
println("\(location)")

//checks if someone touched the menu button
if menuButton.containsPoint(location) {
println("Play story with sound!")
settings.setInProgress()
goToPauseMenu()
}

if soundButton.containsPoint(location) {
println("Play story with sound!")
settings.setInProgress()
runAction(playNar)
}

//checks if someone touched the forward button
if pageForward.containsPoint(location) {
println("Next Page!")
settings.setInProgress()
nextPage()
}

}
}

Aucun commentaire:

Enregistrer un commentaire