停车时,音频引擎正在崩溃



在调用停止音频引擎功能时,音频引擎正在崩溃。

以下是我要启动的功能,也可以停止音频引擎。

 public func start_record() {
    if audioEngine.isRunning {
        regonised_text = ""
        print("audio engine running")
        audioEngine.inputNode?.removeTap(onBus: 0)
        audioEngine.stop()
        recognitionRequest?.endAudio()
         audioEngine.reset()
    } else {
         print("audio engine not running")
        speech_reconginiser.text = ""
        speech_reconginiser.result = false
         startRecording()
    }
}

此函数用于启动_ret_record。

 public func startRecording() {
        if recognitionTask != nil {
            recognitionTask?.cancel()
            recognitionTask = nil
        }
        let audioSession = AVAudioSession.sharedInstance()
        do {
            try audioSession.setCategory(AVAudioSessionCategoryRecord)
            try audioSession.setMode(AVAudioSessionModeMeasurement)
            try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
        } catch {
            print("audioSession properties weren't set because of an error.")
        }
        recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
        guard let inputNode = audioEngine.inputNode else {
            fatalError("Audio engine has no input node")
        }
        guard let recognitionRequest = recognitionRequest else {
            fatalError("Unable to create an SFSpeechAudioBufferRecognitionRequest object")
        }
        recognitionRequest.shouldReportPartialResults = true
        recognitionTask = speechRecognizer.recognitionTask(with: recognitionRequest, resultHandler: { (result, error) in
            var isFinal = false
            if result != nil {
                self.speech_reconginiser.text = ""
                self.regonised_text = (result?.bestTranscription.formattedString)!
                // print("Final text :", self.regonised_text)
                self.speech_reconginiser.result = true
                self.speech_reconginiser.text = self.regonised_text
                // print("Final text 1213 :",self.speech_reconginiser.text)
                isFinal = (result?.isFinal)!
            } else {
                self.speech_reconginiser.text = ""
                self.speech_reconginiser.result = false
                self.speech_reconginiser.text = ""
            }
            if error != nil || isFinal {
                self.audioEngine.stop()
                inputNode.removeTap(onBus: 0)
                self.recognitionRequest = nil
                self.recognitionTask = nil
            }
        })
        let recordingFormat = inputNode.outputFormat(forBus: 0)
        inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
            self.recognitionRequest?.append(buffer)
        }
        audioEngine.prepare()
        do {
            try audioEngine.start()
        } catch {
            print("audioEngine couldn't start because of an error.")
        }
    }

这是堆栈跟踪

   * thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BREAKPOINT (code=1, subcode=0x18cd96ea8)
  * frame #0: 0x000000018cd96ea8 CoreFoundation`___forwarding___ + 744
    frame #1: 0x000000018cc92d4c CoreFoundation`_CF_forwarding_prep_0 + 92
    frame #2: 0x000000018cc72a80 CoreFoundation`-[__NSArrayI dealloc] + 84
    frame #3: 0x000000018b812134 libobjc.A.dylib`(anonymous namespace)::AutoreleasePoolPage::pop(void*) + 836
    frame #4: 0x000000018cc6eb28 CoreFoundation`_CFAutoreleasePoolPop + 28
    frame #5: 0x000000018cc729c0 CoreFoundation`CFRunLoopTimerInvalidate + 616
    frame #6: 0x000000018cd417dc CoreFoundation`__CFRunLoopDoTimer + 908
    frame #7: 0x000000018cd41068 CoreFoundation`__CFRunLoopDoTimers + 244
    frame #8: 0x000000018cd3ec8c CoreFoundation`__CFRunLoopRun + 1484
    frame #9: 0x000000018cc6eda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #10: 0x000000018e6d9074 GraphicsServices`GSEventRunModal + 100
    frame #11: 0x0000000192f29c9c UIKit`UIApplicationMain + 208
    frame #12: 0x0000000100542a2c GraspIO-Dev`main at AppDelegate.swift:22
    frame #13: 0x000000018bc7d59c libdyld.dylib`start + 4
  thread #4
    frame #0: 0x000000018bd8da88 libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x000000018be52fd0 libsystem_pthread.dylib`_pthread_wqthread + 792
    frame #2: 0x000000018be52cac libsystem_pthread.dylib`start_wqthread + 4
  thread #5
    frame #0: 0x0000000103a80c80 libdispatch.dylib`_dispatch_introspection_thread_add + 188
    frame #1: 0x0000000103a65380 libdispatch.dylib`_dispatch_worker_thread3 + 112
    frame #2: 0x000000018be53100 libsystem_pthread.dylib`_pthread_wqthread + 1096
    frame #3: 0x000000018be52cac libsystem_pthread.dylib`start_wqthread + 4
  thread #6, name = 'com.apple.uikit.eventfetch-thread'
    frame #0: 0x000000018bd6f224 libsystem_kernel.dylib`mach_msg_trap + 8
    frame #1: 0x000000018bd6f09c libsystem_kernel.dylib`mach_msg + 72
    frame #2: 0x000000018cd40e90 CoreFoundation`__CFRunLoopServiceMachPort + 192
    frame #3: 0x000000018cd3eae4 CoreFoundation`__CFRunLoopRun + 1060
    frame #4: 0x000000018cc6eda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #5: 0x000000018d789db4 Foundation`-[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 304
    frame #6: 0x000000018d7aab84 Foundation`-[NSRunLoop(NSRunLoop) runUntilDate:] + 96
    frame #7: 0x00000001938b3830 UIKit`-[UIEventFetcher threadMain] + 136
    frame #8: 0x000000018d887318 Foundation`__NSThread__start__ + 996
    frame #9: 0x000000018be5568c libsystem_pthread.dylib`_pthread_body + 240
    frame #10: 0x000000018be5559c libsystem_pthread.dylib`_pthread_start + 284
    frame #11: 0x000000018be52cb4 libsystem_pthread.dylib`thread_start + 4
  thread #8
    frame #0: 0x000000018be52ca8 libsystem_pthread.dylib`start_wqthread
  thread #9
    frame #0: 0x000000018bd8da88 libsystem_kernel.dylib`__workq_kernreturn + 8
    frame #1: 0x000000018be52fd0 libsystem_pthread.dylib`_pthread_wqthread + 792
    frame #2: 0x000000018be52cac libsystem_pthread.dylib`start_wqthread + 4
  thread #10
    frame #0: 0x000000018be52ca8 libsystem_pthread.dylib`start_wqthread
  thread #11, name = 'com.apple.NSURLConnectionLoader'
    frame #0: 0x000000018bd6f224 libsystem_kernel.dylib`mach_msg_trap + 8
    frame #1: 0x000000018bd6f09c libsystem_kernel.dylib`mach_msg + 72
    frame #2: 0x000000018cd40e90 CoreFoundation`__CFRunLoopServiceMachPort + 192
    frame #3: 0x000000018cd3eae4 CoreFoundation`__CFRunLoopRun + 1060
    frame #4: 0x000000018cc6eda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #5: 0x000000018d47bdf4 CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 404
    frame #6: 0x000000018d887318 Foundation`__NSThread__start__ + 996
    frame #7: 0x000000018be5568c libsystem_pthread.dylib`_pthread_body + 240
    frame #8: 0x000000018be5559c libsystem_pthread.dylib`_pthread_start + 284
    frame #9: 0x000000018be52cb4 libsystem_pthread.dylib`thread_start + 4
  thread #12, name = 'com.apple.CFStream.LegacyThread'
    frame #0: 0x000000018bd6f224 libsystem_kernel.dylib`mach_msg_trap + 8
    frame #1: 0x000000018bd6f09c libsystem_kernel.dylib`mach_msg + 72
    frame #2: 0x000000018cd40e90 CoreFoundation`__CFRunLoopServiceMachPort + 192
    frame #3: 0x000000018cd3eae4 CoreFoundation`__CFRunLoopRun + 1060
    frame #4: 0x000000018cc6eda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #5: 0x000000018cd4e794 CoreFoundation`_legacyStreamRunLoop_workThread + 268
    frame #6: 0x000000018be5568c libsystem_pthread.dylib`_pthread_body + 240
    frame #7: 0x000000018be5559c libsystem_pthread.dylib`_pthread_start + 284
    frame #8: 0x000000018be52cb4 libsystem_pthread.dylib`thread_start + 4
  thread #13, name = 'com.apple.CFSocket.private'
    frame #0: 0x000000018bd8d23c libsystem_kernel.dylib`__select + 8
    frame #1: 0x000000018cd47cb0 CoreFoundation`__CFSocketManager + 632
    frame #2: 0x000000018be5568c libsystem_pthread.dylib`_pthread_body + 240
    frame #3: 0x000000018be5559c libsystem_pthread.dylib`_pthread_start + 284
    frame #4: 0x000000018be52cb4 libsystem_pthread.dylib`thread_start + 4
  thread #14
    frame #0: 0x000000018bd6f278 libsystem_kernel.dylib`semaphore_timedwait_trap + 8
    frame #1: 0x0000000103a7f238 libdispatch.dylib`_os_semaphore_timedwait + 92
    frame #2: 0x0000000103a67db8 libdispatch.dylib`_dispatch_semaphore_wait_slow + 72
    frame #3: 0x0000000103a66f8c libdispatch.dylib`_dispatch_worker_thread + 272
    frame #4: 0x000000018be5568c libsystem_pthread.dylib`_pthread_body + 240
    frame #5: 0x000000018be5559c libsystem_pthread.dylib`_pthread_start + 284
    frame #6: 0x000000018be52cb4 libsystem_pthread.dylib`thread_start + 4
  thread #15, name = 'AVAudioSession Notify Thread'
    frame #0: 0x000000018bd6f224 libsystem_kernel.dylib`mach_msg_trap + 8
    frame #1: 0x000000018bd6f09c libsystem_kernel.dylib`mach_msg + 72
    frame #2: 0x000000018cd40e90 CoreFoundation`__CFRunLoopServiceMachPort + 192
    frame #3: 0x000000018cd3eae4 CoreFoundation`__CFRunLoopRun + 1060
    frame #4: 0x000000018cc6eda4 CoreFoundation`CFRunLoopRunSpecific + 424
    frame #5: 0x00000001a6fb54f4 AVFAudio`GenericRunLoopThread::Entry(void*) + 164
    frame #6: 0x00000001a6fdb814 AVFAudio`CAPThread::Entry(CAPThread*) + 84
    frame #7: 0x000000018be5568c libsystem_pthread.dylib`_pthread_body + 240
    frame #8: 0x000000018be5559c libsystem_pthread.dylib`_pthread_start + 284
    frame #9: 0x000000018be52cb4 libsystem_pthread.dylib`thread_start + 4

我找不到它是如何发生的,我在堆栈跨流中进行了相当的搜索,然后我发现由于 input节点,在停止音频引擎时没有获得null 。我遇到的错误是" exc_bad_access(代码= 1,地址= 0x20)"

尝试以下:

    let recordingFormat = inputNode.inputFormat(forBus: 0)
    inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, when) in
        self.recognitionRequest?.append(buffer)
    }
    audioEngine.prepare()
    do {
        try audioEngine.start()
    } catch {
        print("audioEngine couldn't start because of an error.")
    }

基本上将您的recordingFormat表格outputFormat更改为inputFormat

此代码块(上方)还应该在您的recognitionTask块内吗?如果不确定,请尝试将代码放入该块中。

最新更新