How to import audio for an object for a game

I.e. like splashy fish every time you tap it makes a popping bubble sound

how would I do that I'm stuck right now and A'm not understanding ehat is going wrong.

import AVFoundation
let url = NSURL(fileURLWithPath: "file.mp3")
let audioFile: AVAudioFile!
do {
    audioFile = try AVAudioFile(forReading: url, commonFormat: AVAudioCommonFormat.PCMFormatFloat32, interleaved: true)
    let engine = AVAudioEngine()
    let player = AVAudioPlayerNode()
   
    engine.attachNode(player)
    engine.connect(player, to: engine.mainMixerNode, format: AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 2))
    do {
        try engine.start()
        player.scheduleFile(audioFile, atTime: nil, completionHandler: nil)
       
        player.play()
    } catch {
    }
} catch {
}

If you are using SpriteKit, you can make an action which plays sound effects:

let playPopAction = SKAction.playSoundFileNamed("yourPopSoundFile.caf", waitForCompletion: false)

where the file yourPopSoundFile.caf is one of your assets.

Okay I'm stuck on a part with the code like you know how splashy fish and flappy bird make the game over sound when you die and the sound when you tap it the bird makes a sound I'm trying to do something like that, but I can't figure out how I'm stuck trying to make it where when you crash it makes a crash noise and game over and I'm also trying to get it to where you can tap it and it makes a noise when you tap I have the audio I just don't know how to input it.






import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {


var score = 0

var scoreLabel = SKLabelNode()

var gameOverLabel = SKLabelNode()


var goat = SKSpriteNode()

var nite = SKSpriteNode()

var labelHolder = SKSpriteNode()



let goatGroup:UInt32 = 1

let objectGroup:UInt32 = 2

let gapGroup:UInt32 = 0 << 3

let playPopAction = SKAction.playSoundFileNamed("Powerup.mp3", waitForCompletion: false)


var gameOver = 0


var movingObjects = SKNode()



override func didMoveToView(view: SKView) {


self.physicsWorld.contactDelegate = self


self.addChild(movingObjects)


makeBackground()


self.addChild(labelHolder)




scoreLabel.fontName = "Helvertica"

scoreLabel.fontSize = 60

scoreLabel.text = "0"

scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height - 70)

self.addChild(scoreLabel)



var goatTexture = SKTexture(imageNamed: "imgg/goat.png")


goat = SKSpriteNode(texture: goatTexture)

goat.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))


goat.physicsBody = SKPhysicsBody(circleOfRadius: goat.size.height / 2)

goat.physicsBody?.dynamic = true

goat.physicsBody?.allowsRotation = false

goat.physicsBody?.categoryBitMask = goatGroup

goat.physicsBody?.collisionBitMask = objectGroup

goat.physicsBody?.contactTestBitMask = objectGroup

goat.physicsBody?.collisionBitMask = gapGroup





self.addChild(goat)


var ground = SKNode()

ground.position = CGPointMake(0, 0)

ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, 1))

ground.physicsBody?.dynamic = false

ground.physicsBody?.categoryBitMask = objectGroup

self.addChild(ground)


var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: Selector("makePipes"), userInfo: nil, repeats: true)




}


func makeBackground() {


var niteTexture = SKTexture(imageNamed: "imgg/nite.png")



var movenite = SKAction.moveByX(-niteTexture.size().width, y: 0, duration: 9)

var replacenite = SKAction.moveByX(niteTexture.size().width, y: 0, duration: 0)

var moveniteForever = SKAction.repeatActionForever(SKAction.sequence([movenite, replacenite]))


for var i:CGFloat=0; i<4; i++ {

nite = SKSpriteNode(texture: niteTexture)

nite.position = CGPoint(x: niteTexture.size().width/2 + niteTexture.size().width * i, y: CGRectGetMidY(self.frame))

nite.size.height = self.frame.height

nite.runAction(moveniteForever)

movingObjects.addChild(nite)

}

}


func makePipes() {


if (gameOver == 0) {


let gapHeight = goat.size.height * 2.1


var movementAmount = arc4random() % UInt32(self.frame.size.height / 2)


var pipesOffset = CGFloat(movementAmount) - self.frame.size.height / 4



var movePipes = SKAction.moveByX(-self.frame.size.width * 2, y: 0, duration: NSTimeInterval(self.frame.size.width / 100))


var removePipes = SKAction.removeFromParent()


var moveAndRemovePipes = SKAction.sequence([movePipes, removePipes])





var pipe1Texture = SKTexture(imageNamed: "imgg/pipe1.png")

var pipe1 = SKSpriteNode(texture: pipe1Texture)

pipe1.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipe1.size.height / 2 + gapHeight / 2 + pipesOffset)

pipe1.runAction(moveAndRemovePipes)

pipe1.physicsBody = SKPhysicsBody(rectangleOfSize: pipe1.size)

pipe1.physicsBody?.dynamic = false

pipe1.physicsBody?.categoryBitMask = objectGroup

movingObjects.addChild(pipe1)




var pipe2Texture = SKTexture(imageNamed: "imgg/pipe2.png")

var pipe2 = SKSpriteNode(texture: pipe2Texture)

pipe2.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) - pipe2.size.height / 2 - gapHeight / 2 + pipesOffset)

pipe2.runAction(moveAndRemovePipes)

pipe2.physicsBody = SKPhysicsBody(rectangleOfSize: pipe2.size)

pipe2.physicsBody?.dynamic = false

pipe1.physicsBody?.categoryBitMask = objectGroup

movingObjects.addChild(pipe2)

var gap = SKNode()

gap.position = CGPoint(x: CGRectGetMidX(self.frame) + self.frame.size.width, y: CGRectGetMidY(self.frame) + pipesOffset)

gap.runAction(moveAndRemovePipes)

gap.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(pipe1.size.width, gapHeight))

gap.physicsBody?.dynamic = false

gap.physicsBody?.collisionBitMask = gapGroup

gap.physicsBody?.categoryBitMask = gapGroup

gap.physicsBody?.contactTestBitMask = goatGroup

movingObjects.addChild(gap)

}


}

func didBeginContact(contact: SKPhysicsContact) {


if contact.bodyA.categoryBitMask == gapGroup || contact.bodyB.categoryBitMask == gapGroup {

score++

scoreLabel.text = "\(score)"

} else {

if gameOver == 0 {

gameOver = 1

movingObjects.speed = 0

gameOverLabel.fontName = "Helvertica"

gameOverLabel.fontSize = 30

gameOverLabel.text = "Game Over! Tap to play again"

gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))

labelHolder.addChild(gameOverLabel)

}

}



}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

/ Called when a touch begins */


if (gameOver == 0) {


goat.physicsBody?.velocity = CGVectorMake(0, 0)

goat.physicsBody?.applyImpulse(CGVectorMake(0, 110))


} else {

score = 0

scoreLabel.text = "0"

scoreLabel.physicsBody?.dynamic = true

movingObjects.removeAllChildren()

makeBackground()

goat.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))

goat.physicsBody?.velocity = CGVectorMake(0, 0)

labelHolder.removeAllChildren()

gameOver = 0

movingObjects.speed = 1

}


}


override func update(currentTime: CFTimeInterval) {

/ Called before each frame is rendered */

}

}

I tried it but it kept crashing the game

How to import audio for an object for a game
 
 
Q