iOS---語音轉文字
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
相關文章
- 語音轉文字工具,語音轉文字怎樣轉?
- chrome語音文字互轉Chrome
- 如何用Python語音合成,以及文字轉語音~Python
- [js常用]文字轉化成語音JS
- C# TTS-文字轉語音C#TTS
- AVFoundation 文字轉語音和音訊錄製 播放音訊
- Windows部署語音轉文字專案_WhisperWindows
- 前端語音轉文字實踐總結前端
- 蘋果手機文字轉語音方法蘋果
- 文字轉語音軟體 VPot v2411
- gTTS: 強大的Python文字轉語音庫TTSPython
- 文字語音互相轉換系統設計
- 如何在Python中將語音轉換為文字Python
- 語音轉文字 從裡面擷取出時間
- 5 款不錯的開源語音識別/語音文字轉換系統
- 怎麼將錄音轉文字,錄音轉文字助手如何使用
- 文字到語音(tts)TTS
- 音訊轉錄文字音訊
- Python音訊轉文字Python音訊
- 構建一個語音轉文字的WebApi服務WebAPI
- Premiere Pro 2022離線語音轉文字教程,圖文!REM
- 快速實現語音轉文字,還自帶翻譯
- 搜狗輸入法語音轉文字型驗報告
- TTS 擂臺: 文字轉語音模型的自由搏擊場TTS模型
- Fish Speech 更新V1.5:領先的多語言文字轉語音模型模型
- AI影片語音轉寫文字工具:AI Transcription for Mac中文版AIMac
- JS實現將文字轉換為語音並自動播放JS
- [js常用]百度將文字轉化為語音例項JS
- 有這5款開源軟體,語音轉文字很簡單!
- 微軟利用AI技術使文字轉語音只需20分鐘微軟AI
- OpenAI Java SDK——chatgpt-java-v1.0.3更新支援GPT-3.5-Turbo,支援語音轉文字,語音翻譯。OpenAIJavaChatGPT
- aardio實現語音閱讀文字【包含選擇語音庫】
- Piper: 快速、本地化的神經網路文字轉語音系統神經網路
- i人的福音!一個強大開源的文字轉語音工具!
- Mac入門科普:如何在 Mac 上開啟語音輸入轉文字Mac
- macOS自帶的免費文字轉語音功能,只需兩行程式碼Mac行程
- 配音員起訴TikTok ikTok對文字轉語音功能配音作出調整
- 手機端搜狗輸入法語音轉文字的體驗報告