gpt4 book ai didi

swift - 如何在 Swift 中停止/取消 playSoundFileNamed?

转载 作者:搜寻专家 更新时间:2023-10-31 23:03:49 27 4
gpt4 key购买 nike

我正在尝试制作一个按钮,按下时会播放声音,再次按下时声音会停止。我正在使用 playSoundFileNamed: runAction: withKey:removeActionForKey,但它不起作用。

我的另一个问题是,是否有一种方法不仅可以停止声音,还可以暂停它(这样它就会从暂停的同一部分开始,而不是从头开始)。我在 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)) ....

//^我只是在这里减少了很多路径 - 按钮被绘制并且它是可插入的/可行的。

    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)
}

//所以,这对我来说是一个主要的无赖:

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()

}
}
}

最佳答案

使用AVAudioPlayer .它允许您开始、停止、暂停、控制音量、循环次数和其他功能。

关于swift - 如何在 Swift 中停止/取消 playSoundFileNamed?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29546063/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com