我为这个“愚蠢”的问题提前道歉,但我觉得我已经耗尽了所有的资源。我几乎没有使用Swift和编码的经验,但根据过去的经验和使用基于对象的编程(如MAX MSP ),我理解了很多。
我正在尝试开发一个用于macOS QuickTime Player录制功能的摄像头/麦克风捕获iOS应用程序(满足我自己访问原始摄像头的需求,因为我确实找不到合适的东西!)。
在成功实现AVCaptureSession视频输出后,我尝试了许多将音频发送到Quicktime (包括AVAudioSessionPortUSBAudio)的方法,但都无济于事。那是在我意识到QuickTime自动捕获iOS系统的音频输出之前。
所以我的假设是我应该能够很容易地在AVCapture会话下预览音频;但事实并非如此!在swift4中似乎是“不可用”中的AVCaptureAudioPreviewOutput,或者我只是错过了一些基本的东西。我在堆栈上看到的文章提到需要停止音频处理,所以我希望它很容易预览/监控。
你们谁能给我介绍一种在AVCaptureSession中预览音频的方法?我仍然有一个实例化的AVAudioSession (我最初的尝试),并且刚刚设法(我希望)成功地将麦克风连接到AVCaptureSession。但是,我不确定还能用什么!我的目标是:只是为了听到系统音频输出上的麦克风输入: Quicktime连接应该(希望)能够处理从USB端口捕获(当iOS设备被选为麦克风时,手机上播放的音乐通过usb )。
let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio)
do {
let audioInput = try AVCaptureDeviceInput(device: audioDevice!)
self.captureSession.addInput(audioInput)
} catch {
print("Unable to add Audio Device")
}我还尝试了其他我正在迷失的东西;
captureSession.automaticallyConfiguresApplicationAudioSession = true
func showAudioPreview() -> Bool { return true }也许可以在捕获的同时使用AVAudioSession?然而,我的基本知识表明,同时运行捕获和音频会话会出现问题。
任何帮助都将受到真诚的感谢,我相信你们中的许多人都会翻白眼,并能够很容易地指出我的错误!
谢谢,
Iwan
发布于 2021-01-21 06:00:23
AVCaptureAudioPreviewOutput只在mac上可用,但你也可以使用AVSampleBufferAudioRenderer。您必须手动将AVCaptureAudioDataOutput可以提供的音频CMSampleBuffer排入队列:
import UIKit
import AVFoundation
class ViewController: UIViewController, AVCaptureAudioDataOutputSampleBufferDelegate {
let session = AVCaptureSession()
let bufferRenderSyncer = AVSampleBufferRenderSynchronizer()
let bufferRenderer = AVSampleBufferAudioRenderer()
override func viewDidLoad() {
super.viewDidLoad()
bufferRenderSyncer.addRenderer(bufferRenderer)
let audioDevice = AVCaptureDevice.default(for: .audio)!
let captureInput = try! AVCaptureDeviceInput(device: audioDevice)
let audioOutput = AVCaptureAudioDataOutput()
audioOutput.setSampleBufferDelegate(self, queue: DispatchQueue.main) // or some other dispatch queue
session.addInput(captureInput)
session.addOutput(audioOutput)
session.startRunning()
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
bufferRenderer.enqueue(sampleBuffer)
if bufferRenderSyncer.rate == 0 {
bufferRenderSyncer.setRate(1, time: sampleBuffer.presentationTimeStamp)
}
}
}https://stackoverflow.com/questions/48525998
复制相似问题