zoukankan      html  css  js  c++  java
  • iOS10原生的语音转文字功能

    #import <Foundation/Foundation.h>
    #import <Speech/Speech.h>
    
    @interface SpeechListener : NSObject
    <SFSpeechRecognitionTaskDelegate,SFSpeechRecognizerDelegate>
    
    @property(nonatomic,strong)SFSpeechRecognizer *listener;
    @property(nonatomic,strong)SFSpeechRecognitionTask *currentTask;
    @property(nonatomic,assign)SFSpeechRecognizerAuthorizationStatus authorizationStatus;
    @property(nonatomic,strong)SFSpeechAudioBufferRecognitionRequest *currentRequest;
    @property(nonatomic,assign)BOOL inRecognizer;
    @property(nonatomic,copy)void (^haveRecognizerSpeakTextBlock)(NSString *text,NSTimeInterval startLocation,NSTimeInterval length);
    
    -(void)startRecognizer;
    -(void)giveBuffer:(CMSampleBufferRef)buffer;
    -(void)endRecognizer;
    
    @end
    #import "SpeechListener.h"
    
    @interface SpeechListener()
    
    @property(nonatomic,copy)NSString *currentText;
    @property(nonatomic,assign)NSTimeInterval beginListenLocation;
    
    @end
    
    @implementation SpeechListener
    
    -(instancetype)init
    {
        self = [super init];
        if (self) {
            self.listener = [[SFSpeechRecognizer alloc] initWithLocale:[NSLocale localeWithLocaleIdentifier:@"zh-CN"]];
            [self.listener setDelegate:self];
            [self requestAuthorizationStatus];
        }
        return self;
    }
    
    -(void)requestAuthorizationStatus
    {
        typeof(self) __weak weakself = self;
        [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
            typeof(weakself) __strong strongself = weakself;
            strongself.authorizationStatus = status;
        }];
    }
    
    -(void)startRecognizer
    {
        self.beginListenLocation = CFAbsoluteTimeGetCurrent();
        self.currentRequest = [[SFSpeechAudioBufferRecognitionRequest alloc] init];
        [self.currentRequest setShouldReportPartialResults:YES];
        typeof(self) __weak weakself = self;
        self.currentTask = [self.listener recognitionTaskWithRequest:self.currentRequest resultHandler:^(SFSpeechRecognitionResult *result, NSError *error) {
            typeof(weakself) __strong strongself = weakself;
            if (strongself.inRecognizer) {
                if (error != nil) {
                    [strongself endRecognizer];
                }
                else
                    if(result != nil) {
                    if (strongself.inRecognizer) {
                        strongself.currentText = [[result bestTranscription] formattedString];
                    }
                    if ([result isFinal]) {
                        [strongself endRecognizer];
                    }
                }
            }
            if (error) {
    
            }
        }];
        self.inRecognizer = YES;
    }
    
    -(void)endRecognizer
    {
        if (self.currentText.length > 0) {
            if (self.haveRecognizerSpeakTextBlock) {
                self.haveRecognizerSpeakTextBlock(self.currentText, self.beginListenLocation, CFAbsoluteTimeGetCurrent() - self.beginListenLocation);
            }
        }
        
        self.inRecognizer = NO;
        if (self.currentTask) {
            [self.currentTask cancel];
            self.currentTask = nil;
        }
        if (self.currentRequest) {
            [self.currentRequest endAudio];
            self.currentRequest = nil;
        }
        self.currentText = @"";
    }
    
    -(void)speechRecognizer:(SFSpeechRecognizer *)speechRecognizer availabilityDidChange:(BOOL)available
    {
        self.inRecognizer = !available;
    }
    
    -(void)giveBuffer:(CMSampleBufferRef)buffer
    {
        if (self.currentRequest) {
            [self.currentRequest appendAudioSampleBuffer:buffer];
        }
        if (self.inRecognizer && (CFAbsoluteTimeGetCurrent() - self.beginListenLocation) > 30) {
            [self endRecognizer];
            [self startRecognizer];
        }
    }
    
    @end

     https://www.politepix.com/openears/   一款免费的识别工具

  • 相关阅读:
    验证整数 Double 转 int 两种写法
    0 can't find referenced pointcut declarePointExpress
    nested exception is java.lang.IllegalArgumentException: Pointcut is not well-formed
    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc
    嵌入式系统(胡威)2019年春第一次作业 参考答案
    PAT(B) 1021 个位数统计(Java)
    PAT(B) 1020 月饼(Java)
    PAT(B) 1019 数字黑洞(Java)
    PAT(B) 1018 锤子剪刀布(C:20分,Java:18分)
    PAT(B) 1017 A除以B(Java)
  • 原文地址:https://www.cnblogs.com/yuxiaoyiyou/p/9570755.html
Copyright © 2011-2022 走看看