I'm trying to make a button, which is when pushed - the sound is played, and when pushed again - the sound would stop. I'm using playSoundFileNamed: runAction: withKey:
and removeActionForKey
, but it doesn't work.
我试着做一个按钮,当按下时——声音被播放,当再次按下时——声音就会停止。我正在使用playSoundFileNamed: runAction: withKey:和removeActionForKey,但是它不起作用。
The other question I have is if there is a way not only to stop the sound, but also to pause it (so that it will start from the same part it was paused, NOT from the beginning). I've seen a similar topic at stack overflow, but yet didn't find an answer on my question yet. Thank you in advance.
我的另一个问题是,是否有一种方法不仅可以停止声音,而且还可以暂停它(这样它就会从被暂停的部分开始,而不是从开始)。我在stack overflow上看到过一个类似的话题,但是还没有找到我的问题的答案。提前谢谢你。
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 just cutting down a lot of path here - the button is drawn and it's pushable/workable.
/ / ^我只是减少大量的路径——绘制按钮和pushable /可行。
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, this is a main bummer for me:
//所以,这对我来说是一个很大的障碍:
func fireturnonoff () {
if firesoundon == false {
var playguitar = SKAction.playSoundFileNamed("guitar.wav", waitForCompletion: true)
runAction(playguitar, withKey: "soundsison")
firesoundon = true
println("firesoundon is == \(firesoundon)")
}
else if firesoundon == true {
removeActionForKey("soundsison")
println("REMOVED")
firesoundon = false
println("firesoundon is == \(firesoundon)")
}
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
for 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()
}
}
}
3 个解决方案
#1
2
Use AVAudioPlayer. It allows you to start, stop, pause, control volume, number of loops and other features.
使用AVAudioPlayer。它允许您启动、停止、暂停、控制卷、循环数和其他特性。
#2
0
New functionality has been added to Swift for SKActions:
Swift增加了新的功能,用于skaction:
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.
它创建一个动作,告诉音频节点停止播放。此操作只能在SKAudioNode对象上执行。音频被停止,如果重新启动,从开始开始。这个动作是不可逆的。
Unfortunately, available only in iOS 9.0 and later.
不幸的是,只能在ios9.0及以后版本中使用。
#3
-1
Now you can stop playing sound action by assigning key string for it then using removeActionForKey
later like this:
现在你可以通过为它分配键字符串来停止播放声音动作,然后像下面这样使用removeActionForKey:
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
我已经测试成功
#1
2
Use AVAudioPlayer. It allows you to start, stop, pause, control volume, number of loops and other features.
使用AVAudioPlayer。它允许您启动、停止、暂停、控制卷、循环数和其他特性。
#2
0
New functionality has been added to Swift for SKActions:
Swift增加了新的功能,用于skaction:
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.
它创建一个动作,告诉音频节点停止播放。此操作只能在SKAudioNode对象上执行。音频被停止,如果重新启动,从开始开始。这个动作是不可逆的。
Unfortunately, available only in iOS 9.0 and later.
不幸的是,只能在ios9.0及以后版本中使用。
#3
-1
Now you can stop playing sound action by assigning key string for it then using removeActionForKey
later like this:
现在你可以通过为它分配键字符串来停止播放声音动作,然后像下面这样使用removeActionForKey:
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
我已经测试成功