2017-07-12 28 views
7

Tôi đang sử dụng API nhận dạng giọng nói iOS từ ứng dụng iOS Mục tiêu-C. Nó hoạt động trên iPhone 6, 7, nhưng không hoạt động trên iPhone 5 (iOS, 10.2.1).Nhận dạng giọng nói trên iPhone 5

Cũng lưu ý rằng nó hoạt động trên iPhone 5s, chỉ cần không phải iPhone 5.

API ngôn luận iOS có hoạt động trên iPhone 5 không? Bạn có phải làm bất cứ điều gì khác nhau để làm cho nó hoạt động, hay không ai biết vấn đề có thể là gì?

Mã cơ bản bên dưới. Không có lỗi xảy ra, và âm lượng mic được phát hiện, nhưng không phát hiện thấy lời nói.

if (audioEngine != NULL) { 
     [audioEngine stop]; 
     [speechTask cancel]; 
     AVAudioInputNode* inputNode = [audioEngine inputNode]; 
     [inputNode removeTapOnBus: 0]; 
    } 

    recording = YES; 
    micButton.selected = YES; 

    //NSLog(@"Starting recording... SFSpeechRecognizer Available? %d", [speechRecognizer isAvailable]); 
    NSError * outError; 
    //NSLog(@"AUDIO SESSION CATEGORY0: %@", [[AVAudioSession sharedInstance] category]); 
    AVAudioSession* audioSession = [AVAudioSession sharedInstance]; 
    [audioSession setCategory: AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker error:&outError]; 
    [audioSession setMode: AVAudioSessionModeMeasurement error:&outError]; 
    [audioSession setActive: true withOptions: AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&outError]; 

    SFSpeechAudioBufferRecognitionRequest* speechRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init]; 
    //NSLog(@"AUDIO SESSION CATEGORY1: %@", [[AVAudioSession sharedInstance] category]); 
    if (speechRequest == nil) { 
     NSLog(@"Unable to create SFSpeechAudioBufferRecognitionRequest."); 
     return; 
    } 

    speechDetectionSamples = 0; 

    // This some how fixes a crash on iPhone 7 
    // Seems like a bug in iOS ARC/lack of gc 
    AVAudioEngine* temp = audioEngine; 
    audioEngine = [[AVAudioEngine alloc] init]; 
    AVAudioInputNode* inputNode = [audioEngine inputNode]; 

    speechRequest.shouldReportPartialResults = true; 

    // iOS speech does not detect end of speech, so must track silence. 
    lastSpeechDetected = -1; 

    speechTask = [speechRecognizer recognitionTaskWithRequest: speechRequest delegate: self]; 

    [inputNode installTapOnBus:0 bufferSize: 4096 format: [inputNode outputFormatForBus:0] block:^(AVAudioPCMBuffer* buffer, AVAudioTime* when) { 
     @try { 
      long millis = [[NSDate date] timeIntervalSince1970] * 1000; 
      if (lastSpeechDetected != -1 && ((millis - lastSpeechDetected) > 1000)) { 
       lastSpeechDetected = -1; 
       [speechTask finish]; 
       return; 
      } 
      [speechRequest appendAudioPCMBuffer: buffer]; 

      //Calculate volume level 
      if ([buffer floatChannelData] != nil) { 
       float volume = fabsf(*buffer.floatChannelData[0]); 

       if (volume >= speechDetectionThreshold) { 
        speechDetectionSamples++; 

        if (speechDetectionSamples >= speechDetectionSamplesNeeded) { 

         //Need to change mic button image in main thread 
         [[NSOperationQueue mainQueue] addOperationWithBlock:^ { 

          [micButton setImage: [UIImage imageNamed: @"micRecording"] forState: UIControlStateSelected]; 

         }]; 
        } 
       } else { 
        speechDetectionSamples = 0; 
       } 
      } 
     } 
     @catch (NSException * e) { 
      NSLog(@"Exception: %@", e); 
     } 
    }]; 

    [audioEngine prepare]; 
    [audioEngine startAndReturnError: &outError]; 
    NSLog(@"Error %@", outError); 
+0

bạn có thể hiển thị mã đã thử –

+0

'SFSpeechRecognizer.requestAuthorization()' trả về cái gì? Nó có thể là một hạn chế trên iPhone 5? Ngoài ra, hãy kiểm tra chế độ tắt tiếng? – Larme

+0

thêm mã, cũng không phải nó hoạt động trên iPhone 5s, vấn đề đã được báo cáo bởi một người sử dụng, tôi không thực sự có một iPhone 5 để kiểm tra trên – James

Trả lời

2

Tôi nghĩ rằng lỗi là ở đây trong mã này:

long millis = [[NSDate date] timeIntervalSince1970] * 1000; 

Các thiết bị 32-bit (iPhone 5 là một thiết bị 32-bit), có thể tiết kiệm số lượng tối đa tối đa 2^32-1 tức là 2.147.483.647.

Tôi đã kiểm tra trên trình giả lập iPhone 5, millis đang có giá trị âm. Trong đoạn mã bạn đã đăng, không có đề cập đến cách lastSpeechDetected đang được đặt sau khi đặt ban đầu thành -1, nhưng nếu bằng cách nào đó ((millis - lastSpeechDetected) > 1000) là đúng, thì nó sẽ nhập if-block và hoàn thành tác vụ thoại.

+1

Vâng, đây là vấn đề. Tôi đã thay đổi mã để sử dụng một "dài dài" và nó làm việc trên iPhone 5. Cảm ơn rất nhiều. – James

+0

@James: Rất vui được trợ giúp. Chúc mừng! –

Các vấn đề liên quan