How to stop/cancel playSoundFileNamed in Swift?

 ̄綄美尐妖づ 提交于 2019-12-06 01:03:34

Use AVAudioPlayer. It allows you to start, stop, pause, control volume, number of loops and other features.

New functionality has been added to Swift for SKActions:

class func stop() -> SKAction

It creates an action that tells an audio node to stop playback. This action may only be executed on an SKAudioNode object. The audio is stopped, and if restarted, begins at the beginning. This action is not reversible.

Unfortunately, available only in iOS 9.0 and later.

Now you can stop playing sound action by assigning key string for it then using removeActionForKey later like this:

let action: SKAction = SKAction.playSoundFileNamed("sounds/boom.wav", waitForCompletion: true)
self.runAction(action, withKey:"die-sound")
// later you do:
self.removeActionForKey("die-sound")

I have tested successful

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!