在调用AudioKit.stop()
之后再调用AudioKit.start()
之后,我无法将AKMixer
/ AKPlayer
重新连接到AudioKit。看来引擎成功启动,但是当我呼叫player.play()
时没有声音产生。我不太确定自己缺少什么。我将其归结为一个简单的演示项目,并在下面包括了最重要的代码部分。
视图控制器
class ViewController: UIViewController {
public let tickSound = MySound(url: "tick.wav")
@IBAction func stopAudioKit(_ sender: Any) {
try! AudioKit.stop()
print("AudioKit stopped")
}
@IBAction func playSound(_ sender: Any) {
tickSound.play()
}
}
声音对象
class MySound: NSObject {
private static var mixer: AKMixer = AKMixer()
private var player: AKPlayer?
init(url: String) {
super.init()
if let file = try? AKAudioFile(readFileName: url) {
self.player = AKPlayer(audioFile: file)
self.player?.buffering = .always
MySound.mixer.connect(input: self.player)
}
}
func play() {
if AudioKit.output !== MySound.mixer {
AudioKit.output = MySound.mixer
}
if !AudioKit.engine.isRunning {
do {
try AudioKit.start()
} catch {
assert(false)
}
}
if AudioKit.engine.isRunning {
player?.play()
} else {
assert(false)
}
}
}
Github演示项目:https://github.com/rednebmas/AudioKitDemo
谢谢!