You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS应用中能否用Swift捕获内部音频?求可行方案

Swift捕获内部音频的实现方案

iOS平台(仅适用于非上架场景)

iOS官方严格限制内部音频捕获,AVFoundation确实只能捕获麦克风输入,但有两种非官方的实现方式:

  • 越狱设备环境:直接使用AudioToolbox框架的RemoteIO Audio Unit,配置音频会话为playAndRecord类别,开启回环模式,通过音频单元的输入回调捕获系统输出的音频数据。这种方式不需要绕太多弯,直接调用Core Audio底层API即可。
  • 非越狱设备调试:需要借助私有API(比如kAudioSessionProperty_OverrideCategoryEnableSystemSoundCapture这类未公开属性),但这种代码会被App Store审核直接拒绝,仅适合个人测试。

macOS平台(可开发,上架需合规)

macOS的音频系统开放度更高,有两种合法方案:

  • AVFoundation + 系统回环设备:
    macOS自带音频回环功能,你可以通过AVCaptureDevice枚举音频输入设备,找到对应的回环设备(名称通常包含“Loopback”),然后用AVCaptureSession捕获该设备的音频流。示例代码:
    import AVFoundation
    
    class AudioCaptureManager: NSObject, AVCaptureAudioDataOutputSampleBufferDelegate {
        private let captureSession = AVCaptureSession()
    
        func startLoopbackCapture() {
            // 搜索音频输入设备
            let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(
                deviceTypes: [.audioInput],
                mediaType: .audio,
                position: .unspecified
            )
            
            // 匹配回环设备(可根据实际设备名称调整)
            guard let loopbackDevice = deviceDiscoverySession.devices.first(where: {
                $0.localizedName.contains("Loopback")
            }) else {
                print("未找到回环音频设备")
                return
            }
            
            do {
                // 添加设备输入
                let audioInput = try AVCaptureDeviceInput(device: loopbackDevice)
                if captureSession.canAddInput(audioInput) {
                    captureSession.addInput(audioInput)
                }
                
                // 添加音频输出处理
                let audioOutput = AVCaptureAudioDataOutput()
                audioOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "AudioCaptureQueue"))
                if captureSession.canAddOutput(audioOutput) {
                    captureSession.addOutput(audioOutput)
                }
                
                captureSession.startRunning()
            } catch {
                print("捕获配置失败:\(error.localizedDescription)")
            }
        }
    
        // 捕获音频数据回调
        func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
            // 在这里处理捕获到的音频数据
            guard let audioBuffer = CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(
                sampleBuffer,
                bufferListSizeNeededOut: nil,
                bufferListOut: nil,
                bufferListSize: MemoryLayout<AudioBufferList>.size,
                blockBufferAllocator: nil,
                blockBufferMemoryAllocator: nil,
                flags: 0,
                blockBufferOut: nil
            ) else { return }
            
            // 示例:打印音频数据格式
            let format = CMSampleBufferGetFormatDescription(sampleBuffer)!
            print("捕获音频格式:\(CMAudioFormatDescriptionGetStreamBasicDescription(format)!.pointee)")
        }
    }
    
  • Core Audio底层API:使用AudioUnit直接配置输出回调,捕获系统音频流,适合需要高度自定义音频处理的场景,步骤包括初始化RemoteIO音频单元、设置输入输出回调、启动音频单元。

新手建议

刚接触Swift的话,优先从macOS平台入手,因为限制少、文档更完善,容易快速看到效果。iOS的非官方方案需要对Core Audio有一定了解,且仅适合个人测试或越狱环境下使用。

内容的提问来源于stack exchange,提问作者CodeSwift13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:57:40