码迷,mamicode.com
首页 > 移动开发 > 详细

iOS10原生的语音转文字功能

时间:2018-09-01 17:36:32      阅读:534      评论:0      收藏:0      [点我收藏+]

标签:art   nil   str   ref   code   property   rtl   ble   available   

#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

 

iOS10原生的语音转文字功能

标签:art   nil   str   ref   code   property   rtl   ble   available   

原文地址:https://www.cnblogs.com/yuxiaoyiyou/p/9570755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!