如果在使用AVAudioEngine时没有声音,可能是由于以下几个原因导致的:
startAndReturnError()
方法来启动音频引擎。let engine = AVAudioEngine()
do {
try engine.start()
} catch let error {
print("Failed to start audio engine: \(error.localizedDescription)")
}
let engine = AVAudioEngine()
let playerNode = AVAudioPlayerNode()
engine.attach(playerNode)
engine.connect(playerNode, to: engine.mainMixerNode, format: engine.mainMixerNode.inputFormat(forBus: 0))
scheduleFile(_:at:completionHandler:)
方法来启动音频节点。let engine = AVAudioEngine()
let playerNode = AVAudioPlayerNode()
engine.attach(playerNode)
engine.connect(playerNode, to: engine.mainMixerNode, format: engine.mainMixerNode.inputFormat(forBus: 0))
let audioFile = try AVAudioFile(forReading: audioURL)
playerNode.scheduleFile(audioFile, at: nil) { }
playerNode.volume = 1.0 // 设置音量为正常值
engine.mainMixerNode.outputVolume = 1.0 // 设置混音器节点的音量为正常值
如果上述解决方法仍然无法解决问题,可以进一步检查音频文件是否存在、格式是否正确,以及其他可能导致没有声音的因素。