美文网首页
iOS---语音转文字

iOS---语音转文字

作者: 彬至睢阳 | 来源:发表于2018-05-26 11:15 被阅读0次

iOS中实现语音转文字,除了一些第三方,如科大讯飞语音、百度语音等第三方的外(这种第三方的在其官方都有详细的教程,这里就不在叙述了。特点是:百度语音支持离线并免费。讯飞的也支持离线,虽然识别率很高也很精准,但是收费),苹果官方也推出了自己的一套识别标准。

苹果官方的文字转语音支持iOS10之后

文字转语音的需求:

1.创建一个按钮用来控制启动、关闭录音

 2.创建一个语音控制器,一个语音识别器 。一个语音任务管理器,

 3.创建显示转换好的文字的控件

步骤:

 iOS语音转文字:1.需要添加Speech.framework库    2.导入头文件#import

 3.在info.plist文件里添加了两个键值Privacy - Microphone Usage Description、Privacy - Speech Recognition Usage Description

具体代码如下:

#import "ViewController.h"

#import<Speech/Speech.h>

#import<AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic,strong)UIButton *swicthBut;

@property (nonatomic,strong)UILabel *labText;

@property(nonatomic,strong)SFSpeechRecognizer*speechRecognizer;//语音识别器

@property (nonatomic,strong) SFSpeechAudioBufferRecognitionRequest *recognitionRequest;//语音识别请求

@property (nonatomic, strong) SFSpeechRecognitionTask *recognitionTask;//语音任务管理器

@property (nonatomic,strong) AVAudioEngine *audioEngine;//语音控制器

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //发送语音认证请求(首先要判断设备是否支持语音识别功能)

    [SFSpeechRecognizer requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {

        boolisButtonEnabled =false;

        switch(status) {

            case SFSpeechRecognizerAuthorizationStatusAuthorized:

                isButtonEnabled =true;

                NSLog(@"可以语音识别");

                break;

            case SFSpeechRecognizerAuthorizationStatusDenied:

                isButtonEnabled =false;

                NSLog(@"用户未授权使用语音识别");

                break;

            case SFSpeechRecognizerAuthorizationStatusRestricted:

                isButtonEnabled =false;

                NSLog(@"语音识别在这台设备上受到限制");

                break;

            case SFSpeechRecognizerAuthorizationStatusNotDetermined:

                isButtonEnabled =false;

                NSLog(@"语音识别未授权");

                break;

            default:

                break;

        }

    }];

    // Do any additional setup after loading the view, typically from a nib.

    self.title=@"测试";

    self.view.backgroundColor = [UIColor whiteColor];

    [self.viewaddSubview:self.swicthBut];

    [self.viewaddSubview:self.labText];

}

- (void)speechRecognizer:(SFSpeechRecognizer*)speechRecognizer availabilityDidChange:(BOOL)available{

    if(available) {

        self.swicthBut.enabled=YES;

          [self.swicthBut setTitle:@"开始录音" forState:UIControlStateNormal];

    }else{

        self.swicthBut.enabled=NO;

          [self.swicthBut setTitle:@"语音识别不可用" forState:UIControlStateNormal];

    }

}

#pragma mark----语音识别

- (SFSpeechRecognizer*)speechRecognizer{

    if (!_speechRecognizer) {

        NSLocale *cale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh-CN"];

       _speechRecognizer= [[SFSpeechRecognizeralloc]initWithLocale:cale];

        //设置代理

        _speechRecognizer.delegate = self;

    }

    return _speechRecognizer;

}

#pragma mark---停止录音

- (void)endRecording{

    [self.audioEngine stop];

    if (_recognitionRequest) {

        [_recognitionRequest endAudio];

    }

    if (_recognitionTask) {

        [_recognitionTask cancel];

        _recognitionTask = nil;

    }

    self.swicthBut.enabled=NO;

}

#pragma  mark---开始录音

- (void)startRecording{

    if (self.recognitionTask) {

        [self.recognitionTask cancel];

        self.recognitionTask = nil;

    }

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];

    NSError*error;

    bool  audioBool = [audioSessionsetCategory:AVAudioSessionCategoryRecorderror:&error];

    NSParameterAssert(!error);

    bool  audioBool1= [audioSessionsetMode:AVAudioSessionModeMeasurementerror:&error];

      NSParameterAssert(!error);

    bool  audioBool2= [audioSessionsetActive:true withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];

      NSParameterAssert(!error);

    if(audioBool || audioBool1||  audioBool2) {

        NSLog(@"可以使用");

    }else{

        NSLog(@"这里说明有的功能不支持");

    }

    self.recognitionRequest = [[SFSpeechAudioBufferRecognitionRequest alloc]init];

    AVAudioInputNode *inputNode = self.audioEngine.inputNode;

    NSAssert(inputNode,@"录入设备没有准备好");

    NSAssert(self.recognitionRequest, @"请求初始化失败");

    self.recognitionRequest.shouldReportPartialResults = true;

     __weaktypeof(self) weakSelf =self;

    //开始识别任务

    self.recognitionTask = [self.speechRecognizer recognitionTaskWithRequest:self.recognitionRequest resultHandler:^(SFSpeechRecognitionResult * _Nullable result, NSError * _Nullable error) {

        __strongtypeof(weakSelf) strongSelf = weakSelf;

        boolisFinal =false;

        if(result) {

            strongSelf.labText.text= [[resultbestTranscription]formattedString];//语音转文本

            isFinal = [resultisFinal];

        }

        if(error || isFinal) {

            [strongSelf.audioEnginestop];

            [inputNoderemoveTapOnBus:0];

            strongSelf.recognitionRequest=nil;

            strongSelf.recognitionTask=nil;

            [strongSelf.swicthButsetTitle:@"开始录音"forState:UIControlStateNormal];

            strongSelf.swicthBut.enabled=true;

        }

    }];

    AVAudioFormat*recordingFormat = [inputNodeoutputFormatForBus:0];

    //在添加tap之前先移除上一个  不然有可能报"Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio',"之类的错误

    [inputNoderemoveTapOnBus:0];

    [inputNodeinstallTapOnBus:0bufferSize:1024format:recordingFormatblock:^(AVAudioPCMBuffer*_Nonnullbuffer,AVAudioTime*_Nonnullwhen) {

         __strongtypeof(weakSelf) strongSelf = weakSelf;

        if(strongSelf.recognitionRequest) {

            [strongSelf.recognitionRequestappendAudioPCMBuffer:buffer];

        }

    }];

    [self.audioEngine prepare];

    boolaudioEngineBool = [self.audioEnginestartAndReturnError:&error];

     NSParameterAssert(!error);

    self.labText.text = @"正在录音。。。";

    NSLog(@"%d",audioEngineBool);

}

#pragma mark---创建录音引擎

- (AVAudioEngine*)audioEngine{

    if (!_audioEngine) {

        _audioEngine= [[AVAudioEnginealloc]init];

    }

    return _audioEngine;

}

#pragma mark----显示控件

- (UILabel*)labText{

    if(!_labText) {

        _labText= [[UILabelalloc]init];

        _labText.frame = CGRectMake(0, 140, [UIScreen mainScreen].bounds.size.width, 50);

        _labText.font= [UIFontsystemFontOfSize:13.0f];

        _labText.numberOfLines = 0;

        _labText.textAlignment = NSTextAlignmentCenter;

        _labText.textColor = [UIColor yellowColor];

    }

    return _labText;

}

#pragma mark----开关

- (UIButton*)swicthBut{

    if (!_swicthBut) {

        _swicthBut= [[UIButtonalloc]init];

        _swicthBut.frame=CGRectMake(50,100,80,30);

        _swicthBut.titleLabel.textAlignment = NSTextAlignmentCenter;

        [_swicthBut setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

        [_swicthBut addTarget:self action:@selector(switchOn:) forControlEvents:UIControlEventTouchUpInside];

        [_swicthBut setTitle:@"开始录音" forState:UIControlStateNormal];

    }

    return _swicthBut;

}

- (void)switchOn:(id)sender{

    if([self.audioEngineisRunning]) {

        [self endRecording];

       [_swicthBut setTitle:@"开始录音" forState:UIControlStateNormal];

    }else{

        [self startRecording];

         [_swicthBut setTitle:@"关闭" forState:UIControlStateNormal];

    }

}

#pragma mark---识别本地音频文件

- (void)recognizeLocalAudioFile:(UIButton*)sender {

    NSLocale *local =[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

    SFSpeechRecognizer *localRecognizer =[[SFSpeechRecognizer alloc] initWithLocale:local];

    NSURL *url =[[NSBundle mainBundle] URLForResource:@"录音.m4a" withExtension:nil];

    if(!url)return;

    SFSpeechURLRecognitionRequest *res =[[SFSpeechURLRecognitionRequest alloc] initWithURL:url];

    __weak typeof(self) weakSelf = self;

    [localRecognizerrecognitionTaskWithRequest:resresultHandler:^(SFSpeechRecognitionResult*_Nullableresult,NSError*_Nullableerror) {

        if(error) {

            NSLog(@"语音识别解析失败,%@",error);

        }

        else

        {

            weakSelf.labText.text = result.bestTranscription.formattedString;

        }

    }];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

相关文章

  • iOS---语音转文字

    iOS中实现语音转文字,除了一些第三方,如科大讯飞语音、百度语音等第三方的外(这种第三方的在其官方都有详细的教程,...

  • iOS---文字转语音

    1.导入导入 AVFoundation框架 2.导入头文件#import AVSpeechSynthesizerD...

  • 免费在线文字转语音播音系统

    抖兔文字转语音助手—软件简介。 文字转语音助手官网,文转语音合成软件,文字转mp3软件,发音接近真人,在线免费试用...

  • 语音转文字如何转?语音转文字软件推荐

    近期一直在帮助朋友整理语音文件,麻烦的是还是将语音内容一点一点的转换成语音内容,整理没有多少的时候,就没有耐心在继...

  • Window文字转语音,转wav

    老旧玩意,突然要用,写个小工具整理成wav .net framework 引用COMMicrosoft Speec...

  • a2 语音翻译文字

    大家好 我们来看一下语音翻译 也可以说语音翻译文字 视频翻译文字 我们来看一下 打开极简字幕 然后 我们点击语音转...

  • 挖到宝啦,好多人要的软件,免费搞来了!

    大家好,我是小阿浩~ 这几天好多人问我有没有 语音转文字工具 今天给大家分享 一款语音转文字软件 一个多功能语音转...

  • 语音文件转成文字怎么转

    语音文件转成文字怎么转,对于语音文件而言,有时候会出现一些声音,在某一些场合,语音文件是一种很不适宜的文件,在这个...

  • ios文字转语音后台(转,待验证)

    最近项目刚刚交付,偶然间用到了语音播报和语音搜索的功能。语音搜索我用的是讯飞的demo,感觉效果还不错,感兴趣的话...

  • 语音文件转成文字格式怎么转

    在参加一些会议或者讲座时,因为来不及记录笔记,所以采用先录音,再将其转换成文字的方式记录会议内容,这种方式也比较的...

网友评论

      本文标题:iOS---语音转文字

      本文链接:https://www.haomeiwen.com/subject/cpnhjftx.html