工程結(jié)構(gòu):

-
導(dǎo)入SDK的框架, 路徑如下
836864F0-4A39-4BF2-9161-A88B279E2F23.png 根據(jù)下面指示, 查看是否成功導(dǎo)入上面的框架,(如果導(dǎo)入方法不正確, 箭頭所指數(shù)據(jù), 不會出現(xiàn))

3.導(dǎo)入其他所需類庫

接下來是代碼實現(xiàn)部分
第一部分: APPDelegate文件
1.在AppDelegate.m文件里面導(dǎo)入相應(yīng)的類庫
#import <iflyMSC/IFlySpeechUtility.h> //導(dǎo)入頭文件```
>2.初始化即創(chuàng)建語音配置對象,只有初始化后才可以使用MSC的各項服務(wù)。建議將初始化放在程序入口處. 此處放在 didFinishLaunchingWithOptions方法中
//傳入Appid
NSString *strId = [NSString stringWithFormat:@"appid = %@", @"5791c885"];
//傳入(需要導(dǎo)入頭文件 <iflyMSC/IFlySpeechUtility.h>)
[IFlySpeechUtility createUtility:strId];```
注意:此接口在非主進程調(diào)用會返回null對象,如需在非主進程使用語音功能,請使用參數(shù):SpeechConstant.*APPID* +"=12345678," + SpeechConstant.*FORCE_LOGIN *+"=true"。
第二部分: FirstViewController, 實現(xiàn)語音--> 文本過程
1.在需要實現(xiàn)該功能的控制器中導(dǎo)入所需的頭文件
//導(dǎo)入頭文件
//科大訊飛語音識別功能回調(diào)方法的接口文件(協(xié)議)
#import <iflyMSC/IFlyRecognizerViewDelegate.h>
//語音識別功能的聲音識別視圖
#import <iflyMSC/IFlyRecognizerView.h>
//語音識別功能中的自定義常量
#import <iflyMSC/IFlySpeechConstant.h>```
2.遵循協(xié)議 , 聲明響應(yīng)屬性
@interface FirstViewController ()<IFlyRecognizerViewDelegate> //遵循協(xié)議
//用于顯示文本
@property (strong, nonatomic) UITextView *showTextView;
//聲明語音識別視圖屬性
@property (strong, nonatomic) IFlyRecognizerView *iflyRecognizerView;
//接收語音結(jié)果的數(shù)組
@property (strong, nonatomic) NSMutableString *resultString;
@end```
3.viewDidLoad方法, 初始化控件, 創(chuàng)建聲音識別視圖, 設(shè)置代理(上面遵循的協(xié)議)
- (void)viewDidLoad {
[super viewDidLoad];
# 第一部分: 初始化控件, 用于顯示轉(zhuǎn)換后的文本信息
self.view.backgroundColor = [UIColor colorWithRed:0.458 green:0.810 blue:1.000 alpha:1.000];
self.showTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width - 20, 300)];
self.showTextView.backgroundColor = [UIColor whiteColor];
self.showTextView.font = [UIFont systemFontOfSize:22];
[self.view addSubview:self.showTextView];
#第二部分: 給按鈕添加事件, 觸發(fā)事件開始轉(zhuǎn)換
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(100, 100, 150, 50)];
[button1 setBackgroundColor:[UIColor greenColor]];
[button1 setTitle:@"語音轉(zhuǎn)文字" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button1 setTag:1001];
button1.titleLabel.font = [UIFont systemFontOfSize:22];
[button1 addTarget:self action:@selector(button1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
#第三部分: 初始化聲音識別視圖
self.iflyRecognizerView = [[IFlyRecognizerView alloc] initWithCenter:self.view.center];
//設(shè)置代理
self.iflyRecognizerView.delegate = self;
}
4.實現(xiàn)button事件, 用來調(diào)用開始聽寫方法
- (void)button1{
//1. 開始聽寫
[self startListenning];
}```
5.實現(xiàn)開始聽寫方法
2. 實現(xiàn)
-
(void)startListenning{
//設(shè)置語音識別結(jié)果應(yīng)用為普通文本領(lǐng)域-->@"iat"標(biāo)識
[self.iflyRecognizerView setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];//設(shè)置前端點檢測時間為5000ms
[self.iflyRecognizerView setParameter:@"5000" forKey:[IFlySpeechConstant VAD_BOS]];//設(shè)置后斷點檢測最大超出時間
[self.iflyRecognizerView setParameter:@"700" forKey:[IFlySpeechConstant VAD_EOS]];//采樣率
[self.iflyRecognizerView setParameter:@"8000" forKey:[IFlySpeechConstant SAMPLE_RATE]];//設(shè)置返回結(jié)構(gòu)中包含的標(biāo)點符號
[self.iflyRecognizerView setParameter:@"1" forKey:[IFlySpeechConstant ASR_PTT]];//設(shè)置語音識別完成后數(shù)據(jù)返回數(shù)據(jù)結(jié)構(gòu)類型 @"plain" 代表 jison
[self.iflyRecognizerView setParameter:@"plain" forKey:[IFlySpeechConstant RESULT_TYPE]];//設(shè)置在document文件夾下存儲的文件名為"temp,asr"
[self.iflyRecognizerView setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];NSString *str = [IFlySpeechConstant ASR_AUDIO_PATH];
NSLog(@"++++++++++++++ document = %@ %@", str, [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]);
//設(shè)置自定義參數(shù)
[self.iflyRecognizerView setParameter:@"custom" forKey:[IFlySpeechConstant PARAMS]];
//開始識別語音
[self.iflyRecognizerView start];
}
6.實現(xiàn)協(xié)議方法, 該方法中接收聽寫的數(shù)據(jù)
-(void)onResult:(NSArray *)resultArray isLast:(BOOL)isLast{
//初始化結(jié)果字符串
self.resultString = [[NSMutableString alloc] init];
//數(shù)據(jù)是以字典存放到數(shù)組中的
NSDictionary *dic = [resultArray objectAtIndex:0];
for (NSString *str in dic) {
[self.resultString appendFormat:@"%@", str];
}
//自定義控件的顯示
self.showTextView.text = [NSString stringWithFormat:@"%@%@", self.showTextView.text, self.resultString];
}```
協(xié)議方法, 如果聽寫錯誤, 會觸發(fā)該方法
- (void)onError:(IFlySpeechError *)error{
NSLog(@"失敗");
}```
***
第三部分: SecondViewController文件, 實現(xiàn)文本--> 語音的轉(zhuǎn)換
1. 導(dǎo)入語言合成的頭文件(工程結(jié)構(gòu)中展示了)
import "SecondViewController.h"
//需要添加的頭文件
import <iflyMSC/IFlyMSC.h>
import "PcmPlayer.h"
import "TTSConfig.h"
typedef NS_OPTIONS(NSInteger, SynthesizeType){
NomalType =5, //普通合成
UrlType = 6 //url合成
};
@interface SecondViewController ()<IFlySpeechSynthesizerDelegate> //遵循語音合成回調(diào)協(xié)議
@property (strong, nonatomic) UITextView *readTextView;
pragma mark ------- 屬性 ----------
//語音合成對象
@property (strong, nonatomic) IFlySpeechSynthesizer *iflySpeechSynthesizer;
//用于播放音頻的對象
@property (strong, nonatomic) PcmPlayer *audioPlayer;
//合成方式(枚舉類型)
@property (assign, nonatomic) SynthesizeType synType;
//合成過程中是否出現(xiàn)錯誤
@property (assign, nonatomic) BOOL hasError;
@end
@implementation SecondViewController
-
(void)viewDidLoad {
[super viewDidLoad];第一部分 : 定義布局
self.view.backgroundColor = [UIColor colorWithRed:1.000 green:0.640 blue:0.774 alpha:1.000];
self.readTextView = [[UITextView alloc] initWithFrame:CGRectMake(10, 200, self.view.frame.size.width - 20, 300)];
self.readTextView.backgroundColor = [UIColor whiteColor];
self.readTextView.font = [UIFont systemFontOfSize:22];[self.view addSubview:self.readTextView];
//開始轉(zhuǎn)換
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[button1 setFrame:CGRectMake(100, 100, 150, 50)];
[button1 setBackgroundColor:[UIColor greenColor]];
[button1 setTitle:@"語音轉(zhuǎn)文字" forState:UIControlStateNormal];
[button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button1 setTag:1001];
button1.titleLabel.font = [UIFont systemFontOfSize:22];
[button1 addTarget:self action:@selector(button1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
#第二部分: 調(diào)用方法
[self initMakeVoice];
}
-
(void)button1{
NSLog(@"開始");if([self.readTextView.text isEqualToString:@""]){
return;}
//如果點擊按鈕時, 正在播放,則結(jié)束播放
if (_audioPlayer != nil && _audioPlayer.isPlaying == YES) {[_audioPlayer stop];}
//設(shè)置合成語音的方式為普通方式
_synType = NomalType;
self.hasError = NO;
//添加代理
_iflySpeechSynthesizer.delegate = self;
//開始播放(指定播放內(nèi)容)
[_iflySpeechSynthesizer startSpeaking:self.readTextView.text];
}
-
(void)initMakeVoice{
TTSConfig *instance = [TTSConfig sharedInstance];
if (instance == nil) {
return ;}
//合成服務(wù)單例
if (_iflySpeechSynthesizer == nil) {_iflySpeechSynthesizer = [IFlySpeechSynthesizer sharedInstance];}
//設(shè)置識別代理
_iflySpeechSynthesizer.delegate = self;//設(shè)置語速 1-100
[_iflySpeechSynthesizer setParameter:instance.speed forKey:[IFlySpeechConstant SPEED]];//設(shè)置音量
[_iflySpeechSynthesizer setParameter:instance.volume forKey:[IFlySpeechConstant VOLUME]];//設(shè)置音調(diào)
[_iflySpeechSynthesizer setParameter:instance.pitch forKey:[IFlySpeechConstant PITCH]];//設(shè)置采用lv
[_iflySpeechSynthesizer setParameter:instance.sampleRate forKey:[IFlySpeechConstant SAMPLE_RATE]];//設(shè)置發(fā)音人
[_iflySpeechSynthesizer setParameter:@"xiaorong" forKey:[IFlySpeechConstant VOICE_NAME]];
}
-
(void)onCompleted:(IFlySpeechError *)error{
NSLog(@"發(fā)生錯誤%@!!!", error);
}```
