gpt4 book ai didi

ios - Swift 声音重复

转载 作者:IT王子 更新时间:2023-10-29 05:28:33 26 4
gpt4 key购买 nike

我有这个功能,我在第一个场景中赋予它生命:

    func ThemeSound() {
if let path = NSBundle.mainBundle().pathForResource("InfinityThemeMain", ofType: "wav") {
let enemy1sound = NSURL(fileURLWithPath:path)
println(enemy1sound)

var error:NSError?
ThemePlayer = AVAudioPlayer(contentsOfURL: enemy1sound, error: &error)
ThemePlayer.prepareToPlay()
ThemePlayer.play()
}
}

但问题是,它只播放一次主题。我需要它重复播放。我以为我知道如何实现这一目标。

runAction(SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock(ThemePlayer), SKAction.waitForDuration(2.55)]))

但是我不能粘贴到 ViewController 中,因为 runAction 需要一个子项来附加它。还有其他方法吗?

最佳答案

只需将 numberOfLoops 属性设置为负数即可。

ThemePlayer.numberOfLoops = -1

来自 the documentation :

numberOfLoops The number of times a sound will return to the beginning, upon reaching the end, to repeat playback.

...

var numberOfLoops: Int

...

A value of 0, which is the default, means to play the sound once. Set a positive integer value to specify the number of times to return to the start and play again. For example, specifying a value of 1 results in a total of two plays of the sound. Set any negative integer value to loop the sound indefinitely until you call the stop method.

您可能还需要 make your AVAudioPlayer a property, rather than a local variable .就目前而言,使用自动引用计数,我认为您的本地 ThemePlayer 实例可能会被 ARC 释放。比我更了解 AVAudioPlayer 的人可能会对此发表评论。

(顺便说一句,您可能希望遵循 Apple 命名对象实例的惯例,以小写字母开头,例如 themePlayer 而不是 ThemePlayer。它会让您的其他人更容易阅读代码。)

关于ios - Swift 声音重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27341835/

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