【问题标题】:Speech recognition on iPhone 5iPhone 5 上的语音识别
【发布时间】:2017-12-16 21:51:25
【问题描述】:

我正在使用来自 Objective-C iOS 应用程序的 iOS 语音识别 API。 它适用于 iPhone 6、7,但不适用于 iPhone 5(iOS,10.2.1)。

另请注意,它适用于 iPhone 5s,但不适用于 iPhone 5。

iOS 语音 API 是否可以在 iPhone 5 上运行?您是否必须采取任何不同的措施才能使其正常工作,或者有人知道问题可能是什么吗?

基本代码如下。未发生错误,检测到麦克风音量,但未检测到语音。

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);

【问题讨论】:

  • 你能展示一下试过的代码吗
  • SFSpeechRecognizer.requestAuthorization() 返回什么?会不会是 iPhone 5 的限制?另外,检查静音模式?
  • 添加了代码,在 iPhone 5s 上也不起作用,这个问题是用户报告的,我实际上没有 iPhone 5 可以测试
  • 是的,看起来很有希望,正在测试中,非常感谢。
  • 我知道这是去年的表格,但有人知道 SpeechDetectionThreshold 和 SpeechDetectionSamplesNeeded 指的是什么吗?

标签: ios objective-c speech-recognition speech-to-text ios10.2


【解决方案1】:

我认为这个代码中的错误在这里:

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

32 位设备(iPhone 5 为 32 位设备),最多可保存 2^32-1 即 2,147,483,647。

我检查了 iPhone 5 模拟器,millis 是负值。在您发布的代码 sn-p 中,没有提及 lastSpeechDetected 在最初将其设置为 -1 后如何设置,但如果 ((millis - lastSpeechDetected) > 1000) 以某种方式为真,那么它将进入 if 块并完成演讲任务。

【讨论】:

  • 是的,这就是问题所在。我将代码更改为使用“long long”,它适用于 iPhone 5。非常感谢。
  • @James:很高兴为您提供帮助。干杯!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多