科大訊飛_語音,文本互轉(zhuǎn)

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


工程結(jié)構(gòu).png
  1. 導(dǎo)入SDK的框架, 路徑如下


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

95968FEF-1129-424F-9743-D2572E98E123.png

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


8D7AB363-C7D5-4D12-91EF-C76D91BDF112.png

接下來是代碼實現(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);
    }```

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,414評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,138評論 25 708
  • 我的夢想 考會計證 目標(biāo)體重 50kg 做個氣質(zhì)女人 加油
    早睡早起的習(xí)慣閱讀 245評論 0 0
  • 第一次聽聞“飛蚊癥”這個名詞大概是年前臘月了 也是一位艾人,平常在家經(jīng)常自灸,后來經(jīng)朋友介紹,體驗了一次我們的熅灸...
    舞陽Wy閱讀 623評論 0 0
  • 恰逢假期臨近,老天爺也開眼了,連續(xù)四五天的陰雨綿綿,竟然說晴就晴了,難得的晴冬暖陽啊,豈能辜負?于是,熱衷戶外行的...
    祥云ZM閱讀 291評論 0 2

友情鏈接更多精彩內(nèi)容