ios - 我的音效循环的代码是什么

标签 ios swift sprite-kit

我正在制作一个有趣的游戏,我想在每次结束时让我的音效循环播放,这些是我到目前为止的代码。

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let touchLocation = touch.location(in: self)
        if atPoint(touchLocation).name == "startGame"{
            let gameScene = SKScene(fileNamed: "GameScene")!
            gameScene.scaleMode = .aspectFill
            view?.presentScene(gameScene, transition: SKTransition.doorsOpenHorizontal(withDuration: TimeInterval(2)))
            self.run(SKAction.playSoundFileNamed("mainmenu.wav", waitForCompletion: false))

最佳答案

您无法使用 SKActions 在场景中播放背景音乐。改用 AVAudioPlayer:

if let filePath = Bundle.main.path(forResource: "mainmenu", ofType: "wav") {
    let url = URL(fileURLWithPath: filePath)
    do {
        let player = try AVAudioPlayer(contentsOf: url)
        player.numberOfLoops = -1
        player.play()
    } catch {
        // catch error if necessary
    }
}

原帖: https://stackoverflow.com/a/23268462/6846532

关于ios - 我的音效循环的代码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900762/

相关文章:

ios - 如何调整 UIScrollView 的大小以适应子 UITextViews 的内容

swift - 如何将字符串转换为 UIColor (Swift)

swift - Linux 导出路径错误替换

sprite-kit - 线程1 : EXEC_BAD_ACCESS (code=1), 地址=0x6000000000000018)

ios - 游戏设计+快速协调跨场景功能

iphone - 从以分钟为单位的数字中检索月、日、小时、分钟

ios - 无法使用 RestKit-0.20.3 在 NSManagedObject 类“XYX”上调用指定的初始值设定项

swift - 如何在窗口中添加 View ,该 View 将在每个 View Controller 的滑动时显示?

swift - 如何最好地在 SpriteKit 中编写撤消函数?

ios - swift中的UIView水平条动画