1

在 WWDC17 上,WatchOS 4 宣布了新的录制功能。我一直在尝试获得 PCM 阵列,但没有成功。

目前我尝试使用以下代码:

检查音频会话:

override func awake(withContext context: Any?) {
    super.awake(withContext: context)

    let recordingSession = AVAudioSession.sharedInstance()

    do {
        try recordingSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
        try recordingSession.setActive(true)
        recordingSession.requestRecordPermission() { [unowned self] allowed in
            DispatchQueue.main.async {
                if allowed {
                    self.recordingSessionSuccessfulPermission()
                } else {
                    self.recordingSessionFailPermission()
                }
            }
        }
    } catch {
        self.recordingSessionFailPermission()
    }
}

开始记录:

@IBAction func startRecordButton() {
    label.setText("start record")
    let audioEngine = AVAudioEngine.init()
    let inputNode = audioEngine.inputNode
    let format = inputNode.inputFormat(forBus: 0)
    inputNode.installTap(onBus: 0, bufferSize: 8000, format: format) { (buf, wheb) in
        self.label.setText("It is working")
    }

    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch {
        // Handle error...
    }
}

此代码引发错误:

[audiocomp] 178: registration server connection invalidated
[audiocomp] 170: registration server proxy error: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service named com.apple.audio.AudioComponentRegistrar was invalidated." UserInfo={NSDebugDescription=The connection to service named com.apple.audio.AudioComponentRegistrar was invalidated.}
4

1 回答 1

2

AudioEngine 对象必须在 Action 之外创建。

例如:

class MyClass: WKInterfaceController {
    private var audioEngine = AVAudioEngine()
于 2017-07-11T14:38:44.840 回答