gpt4 book ai didi

Swift 函数 playAtTime() 不添加延迟

转载 作者:行者123 更新时间:2023-11-28 12:54:56 25 4
gpt4 key购买 nike

我正在尝试播放两个音频播放器,一个接一个播放完毕。我正在使用 Swift 函数 playAtTime() 创建第二个延迟,如下所示:

var audioPlayer1 = AVAudioPlayer()
var audioPlayer2 = AVAudioPlayer()

let soundPathA = NSBundle.mainBundle().pathForResource("A", ofType: "m4a")
let soundURLA = NSURL.fileURLWithPath(soundPathA!)

let soundPathB = NSBundle.mainBundle().pathForResource("B", ofType: "m4a")
let soundURLB = NSURL.fileURLWithPath(soundPathB!)

var noteA = Sound()
noteA.URL = soundURLA

var noteB = Sound()
noteB.URL = soundURLB

self.audioPlayer1 = try!AVAudioPlayer(contentsOfURL: soundURLA)
self.audioPlayer2 = try!AVAudioPlayer(contentsOfURL: soundURLB)

let duration : NSTimeInterval = audioPlayer1.duration
self.audioPlayer1.play()
self.audioPlayer2.playAtTime(duration)

但是,不会发生延迟。我的问题是什么?

最佳答案

playAtTime 函数不会在给定时间开始播放。相反,它会立即播放,从要播放的声音中的给定时间开始。因此,如果您给它 0.6,它会立即开始播放,但声音将从 0.6 秒后开始。请参阅文档 here .

如果你想在播放之前等待,你可以使用dispatch_after:

let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(0.6 * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
self.audioPlayer2.play()
}

关于Swift 函数 playAtTime() 不添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35261702/

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