8

Tôi đang triển khai Nhận dạng giọng nói trong ứng dụng của mình. Khi tôi lần đầu tiên trình bày bộ điều khiển xem với logic nhận dạng giọng nói, mọi thứ hoạt động tốt. Tuy nhiên, khi tôi cố gắng trình bày các điều khiển xem lại, tôi nhận được lỗi sau:Đầu vào AVAudioEngineNút cài đặtTạm dừng khi khởi động lại bản ghi

ERROR: [0x190bf000] >avae> AVAudioNode.mm:568: CreateRecordingTap: required condition is false: IsFormatSampleRateAndChannelCountValid(format) 
*** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: IsFormatSampleRateAndChannelCountValid(format)' 

Dưới đây là mã được sử dụng để khởi động và dừng ghi:

@available(iOS 10.0, *) 
extension DictationViewController { 

fileprivate func startRecording() throws { 
    guard let recognizer = speechRecognizer else { 
     debugLog(className, message: "Not supported for the device's locale") 
     return 
    } 

    guard recognizer.isAvailable else { 
     debugLog(className, message: "Recognizer is not available right now") 
     return 
    } 

    mostRecentlyProcessedSegmentDuration = 0 
    guard let node = audioEngine.inputNode else { 
     debugLog(className, message: "Could not get an input node") 
     return 
    } 

    let recordingFormat = node.outputFormat(forBus: 0) 
    node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { [weak self] (buffer, _) in 
     self?.request.append(buffer) 
    } 

    audioEngine.prepare() 
    try audioEngine.start() 

    recognitionTask = recognizer.recognitionTask(with: request, resultHandler: {/***/}) 
} 

fileprivate func stopRecording() { 
    audioEngine.stop() 
    audioEngine.inputNode?.removeTap(onBus: 0) 
    request.endAudio() 
    recognitionTask?.cancel() 
} 

} 

startRecording() được gọi trong viewDidLoad khi chúng tôi có yêu cầu ủy quyền. stopRecording() được gọi khi bộ điều khiển xem bị loại bỏ.

Vui lòng hỗ trợ. Tôi đang phải vật lộn để tìm một giải pháp cho vụ tai nạn

+0

Bạn có tìm giải pháp này không? Tôi đang gặp sự cố tương tự, nhưng chỉ khi chạy trên iOS 8.1 ?? –

Trả lời

1

Bạn có thể thay thế mã này:

let recordingFormat = node.outputFormat(forBus: 0) 

như sau:

let recordingFormat = AVAudioFormat(standardFormatWithSampleRate: 44100, channels: 1) 

Mã này khắc phục vấn đề.

+0

cảm ơn khắc phục sự cố này. Nhưng lý do mà outputFormat (forBus: 0) không hoạt động là gì? –

0

Đầu tiên, một vấn đề nhỏ. Khi khai thác microphone của thiết bị, bạn sẽ muốn sử dụng định dạng của đầu vào xe buýt:

let recordingFormat = node.inputFormat(forBus: 0) 

Thứ hai, sau khi một số đào nó có vẻ như vụ tai nạn này thường bắt nguồn từ cài đặt danh mục AVAudioSession chia sẻ ứng dụng của bạn. Đảm bảo bạn đã định cấu hình phiên âm thanh của mình như vậy nếu bạn định thực hiện xử lý âm thanh micrô trực tiếp:

private func configureAudioSession() { 
    do { 
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with: .mixWithOthers) 
     try AVAudioSession.sharedInstance().setActive(true) 
    } catch { } 
} 
Các vấn đề liên quan