backgroundMusic =SKAudioNode(fileNamed: "audio.mp3")
backgroundMusic.autoplayLooped = false
addChild(backgroundMusic)
backgroundMusic.runAction(SKAction.play())
After all the audio plays, I need to execute a function, but I don't know how to do it.
Any suggestion?
UPDATE
OPTION 1
I have a button that I click and it makes a SKSpriteNode
call get smaller miaumHelp
, then if I press it again it gets bigger, after getting bigger it sounds an audio, and it gets smaller, but it gets smaller before the audio ends, it doesn't wait for the audio, the idea is which ends the audio and after that it shrinks.
So what I need is that after the audio is complete, it shrinks, not before.
Here my code.
if toggleMiaumHelpState {
toggleMiaumHelpState = false
miaumHelp.runAction(SKAction.scaleTo(0.2, duration: 0.5), completion: { print("small")})
backgroundMusic.runAction(SKAction.stop())
} else{
toggleMiaumHelpState = true
miaumHelp.runAction(
SKAction.scaleTo(1.0, duration: 0.5), completion: {
print("big")
self.backgroundMusic.runAction(
SKAction.play(),
completion:{
//Se ejecuta de inmediato, no espera que termine el audio.
self.miaumHelp.runAction(SKAction.scaleTo(0.3, duration: 0.5), completion: {
print("small again 0.3")
})
}
)
})
}
OPTION 2
I have a button that when I pinch makes a SKSpriteNode
call decrease in size miaumHelp
, then, if I press it again it enlarges, after enlarging it plays an audio, and then it shrinks, here it shrinks after the audio ends, but if I press the button again, I do not have how to stop the audio and that the action is not executed actionSmallAgain
.
Here the audio works, but you can not cancel the sound or the effect that follows.
if toggleMiaumHelpState {
toggleMiaumHelpState = false
miaumHelp.runAction(SKAction.scaleTo(0.2, duration: 0.5), completion: {
print("small")
})
} else{
toggleMiaumHelpState = true
miaumHelp.runAction(SKAction.scaleTo(1, duration: 0.5), completion: {
print("big")
self.runAction(
SKAction.playSoundFileNamed("audio.mp3", waitForCompletion: true)
,completion: {
self.miaumHelp.runAction(SKAction.scaleTo(0.4, duration: 0.5), withKey: "actionSmallAgain")
print("small again")
}
)
})
}
I finally did it with
AVAudioPlayer
which has a function that is executed when an audio ends, called: