iPhone 5上的语音识别

7
我正在使用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 的限制吗?另外,请检查静音模式。 - Larme
我有一个语音识别应用程序,可以在5s上运行。 - mikep
在WWDC 2016的509会议中(https://developer.apple.com/videos/play/wwdc2016/509/),苹果工程师表示它适用于所有运行iOS 10的设备。 - Puneet Sharma
1
是的,看起来很有希望,在测试过程中,非常感谢。 - James
1
我知道这个是去年的问题,但是有人知道speechDetectionThreshold和speechDetectionSamplesNeeded指的是什么吗? - user6631314
显示剩余5条评论
1个回答

2

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

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

32位设备(例如iPhone 5)最多可以保存2^32-1即2,147,483,647个数字。

我在iPhone 5模拟器上进行了检查,发现毫秒数会有负值。在你所发布的代码片段中,没有提到如何设置lastSpeechDetected的值,但如果一些方式使得((millis - lastSpeechDetected) > 1000)为真,则程序将进入if块并完成语音任务。


1
是的,这就是问题所在。我将代码更改为使用“long long”,它在iPhone 5上运行良好。非常感谢。 - James
@James:很高兴能帮忙。干杯! - Puneet Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接