gpt4 book ai didi

ios - swift 2 : Sound on NSTimer Always Plays Twice

转载 作者:行者123 更新时间:2023-11-30 14:00:56 25 4
gpt4 key购买 nike

所以我有一个 NSTimer 每 0.1 秒运行一个函数。

无论我设置了多少条件和警告,这个声音(以及计时器上的其他声音)都会播放两次。由触摸触发的相同声音仅播放一次。

myTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("flip:"), userInfo: nil, repeats: true)

这是它触发的函数。

func flip(timer: NSTimer) {

if current_timer == -3.0 && rang_3 == false {

if rang_3 == false {

rang_3 = true

dialed_3()

NSLog("DIALED 3")
}

}

}

这是我只需要触发一次的声音函数。

func dialed_3() {

if sound_mute == false {

do {

dial_3 = try AVAudioPlayer(contentsOfURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("3", ofType: "mp3")!))
dial_3.volume = 2
dial_3.numberOfLoops = 0
dial_3.prepareToPlay()
dial_3.play()


} catch {

print("Error")

}

}


}

最佳答案

我不知道你的其他代码,但根据你所展示的内容,它运行了1次。给您的一个小建议:将初始化移至全局,而不是每次计时器运行时都运行它。

关于ios - swift 2 : Sound on NSTimer Always Plays Twice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005407/

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