首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >基于iPhone 5的语音识别

基于iPhone 5的语音识别
EN

Stack Overflow用户
提问于 2017-07-12 20:50:00
回答 1查看 382关注 0票数 7

我使用的是Objective-C iOS应用程序中的iOS语音识别应用程序接口。它可以在iPhone 6、7上运行,但不能在iPhone 5 (iOS,10.2.1)上运行。

还要注意的是,它可以在iPhone 5s上运行,但不能在iPhone 5上运行。

iOS speech应用编程接口应该在iPhone 5上工作吗?你是否必须做一些不同的事情来让它工作,或者有人知道问题可能是什么?

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

代码语言:javascript
运行
复制
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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-22 14:58:33

我认为这段代码中的错误是:

代码语言:javascript
运行
复制
long millis = [[NSDate date] timeIntervalSince1970] * 1000;

32位设备(iPhone 5是32位设备),最多可以节省2^32-1,即2,147,483,647。

我在iPhone 5模拟器上检查了一下,millis的值是负值。在您发布的代码片段中,没有提到在最初将lastSpeechDetected设置为-1之后如何设置它,但是如果不知何故((millis - lastSpeechDetected) > 1000)为真,那么它将进入if块并完成语音任务。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45058172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档