【发布时间】:2018-11-16 15:48:52
【问题描述】:
我正在尝试在 swift AVFoundations 中混合两个音频文件。我有一个混合两个音频并同时播放它们的解决方案。我真正想要的是在一段时间后开始第二个音频。例如,音频 1 播放,然后第二个音频在 10(给定时间)秒后开始播放。任何帮助表示赞赏。提前致谢。
private var audioFiles: Array<String>
private var audioEngine: AVAudioEngine = AVAudioEngine()
private var mixer: AVAudioMixerNode = AVAudioMixerNode()
func Play() {
// do work in a background thread
DispatchQueue.global(qos: .background).async {
self.audioEngine.attach(self.mixer)
self.audioEngine.connect(self.mixer, to: self.audioEngine.outputNode, format: nil)
// !important - start the engine *before* setting up the player nodes
try! self.audioEngine.start()
let fileManager = FileManager.default
for audioFile in self.audioFiles {
// Create and attach the audioPlayer node for this file
let audioPlayer = AVAudioPlayerNode()
self.audioEngine.attach(audioPlayer)
// Notice the output is the mixer in this case
self.audioEngine.connect(audioPlayer, to: self.mixer, format: nil)
let fileUrl = NSURL.init(fileURLWithPath: fileName.removingPercentEncoding!)
var file : AVAudioFile
// We should probably check if the file exists here ¯\_(ツ)_/¯
try! AVAudioFile.init(forReading: fileUrl.absoluteURL!)
audioPlayer.scheduleFile(file, at: nil, completionHandler: nil)
audioPlayer.play(at: nil)
}
}
}
【问题讨论】:
标签: ios swift cocoa cocoa-touch avfoundation