【发布时间】:2021-02-24 20:41:58
【问题描述】:
我正在尝试获取我的 Mac 的主音频输出,然后点击它,然后在上面使用语音文本。
我目前卡住的部分是让我的 Mac 的主要声音输出。例如,如果我要去Youtube 播放视频,它会将声音输出到我的笔记本电脑扬声器。我正在尝试利用该输出。
我正在使用
AVAudioEngine.mainMixerNode
但是,我没有得到任何输出。我也试过AVAudioEngine.outputNode
这是我尝试过的:
engine = AVAudioEngine()
let _ = engine.mainMixerNode
engine.prepare()
do {
try engine.start()
} catch {
print("Start Error")
return
}
do {
let settings = engine.mainMixerNode.outputFormat(forBus: 0).settings
print("FileType: \(settings[AVAudioFileTypeKey])") // Returns nil
self.file = try! AVAudioFile(forWriting: url, settings: settings)
engine.mainMixerNode.installTap(onBus: 0, bufferSize: 1024, format: nil) { // I've tried with a format and without
(buffer: AVAudioPCMBuffer?, time: AVAudioTime!) -> Void in
do {
// Buffer must be empty because the CAF audio length is correct, but there's no sound. It's flat.
try self.file.write(from: buffer!)
} catch _{
print("Problem Writing Buffer")
}
}
}
上面的代码设置了Privacy - Microphone Usage Description。 (尽管我不想点击麦克风)
这里的第一个问题是我不确定我的输出应该使用什么文件类型。我试过CAF,但在GarageBand 中打开它没有声音(虽然有长度)
我尝试将文件重命名为m4a 和wav,但Apple 的Music 就是不想播放它。
我做错了什么? 非常感谢任何帮助。
【问题讨论】: