iOS語音辨識,語音播報,文字變語音播報,語音變文字

來源:互聯網
上載者:User

標籤:

首先使用的是科大訊飛的sdk 

1.語音辨識部分

AppDelegate.m

 

#import "AppDelegate.h"

 

#import <iflyMSC/iflyMSC.h>

 

@interface AppDelegate ()

 

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSString *initString = [[NSString alloc] initWithFormat:@"appid=%@",@"你的app ID"];

    [IFlySpeechUtility createUtility:initString];

    return YES;

}

.h檔案

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController

 @end

.m實現檔案

 

#import "ViewController.h"

#import <iflyMSC/iflyMSC.h>

 

 

@interface ViewController ()<IFlySpeechRecognizerDelegate>

{

    //不帶介面的識別對象

    IFlySpeechRecognizer *iFlySpeechRecognizer;

}

 

@property(nonatomic,strong)UILabel * showLabel;

 

@end

 

@implementation ViewController

-(void)viewDidLoad{

    [super viewDidLoad];

    

    self.showLabel = [[UILabel alloc]initWithFrame:CGRectMake(8,200,300,200)];

    self.showLabel.textColor = [UIColor redColor];

    [self.view addSubview:self.showLabel];

    

    //1.建立聽寫對象

    iFlySpeechRecognizer = [IFlySpeechRecognizer sharedInstance]; //設定聽寫模式

    

    iFlySpeechRecognizer.delegate = self;

    [iFlySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];

    //2.設定聽寫參數

    [iFlySpeechRecognizer setParameter: @"iat" forKey: [IFlySpeechConstant IFLY_DOMAIN]];

    //asr_audio_path是錄音檔案名稱,設定value為nil或者為空白取消儲存,預設儲存目錄在 Library/cache下。

    [iFlySpeechRecognizer setParameter:@"asrview.pcm" forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];

 

 

    UIButton * startButton = [[UIButton alloc]initWithFrame:CGRectMake(20,100,100,50)];

    [startButton addTarget:self action:@selector(startButtonDidClick) forControlEvents:UIControlEventTouchUpInside];

    startButton.backgroundColor = [UIColor orangeColor];

    [startButton setTitle:@"開始錄音" forState:UIControlStateNormal];

    [self.view addSubview:startButton];

    

    UIButton * endButton = [[UIButton alloc]initWithFrame:CGRectMake(200,100,100,50)];

    [endButton addTarget:self action:@selector(endButtonDidClick) forControlEvents:UIControlEventTouchUpInside];

    endButton.backgroundColor = [UIColor orangeColor];

    [endButton setTitle:@"結束錄音" forState:UIControlStateNormal];

    [self.view addSubview:endButton];

}

-(void)startButtonDidClick{

    //3.啟動識別服務

    [iFlySpeechRecognizer startListening];

}

-(void)endButtonDidClick{

    //識別服務

    [iFlySpeechRecognizer stopListening];

}

 

 

- (void) onResults:(NSArray *) results isLast:(BOOL)isLast{

    NSMutableString * resultString = [[NSMutableString alloc]init];

    if (!isLast) {

        NSDictionary *dic = results[0];

        NSArray * keys = [dic allKeys];

        for (NSString *key in keys) {

            NSData * resData = [key dataUsingEncoding:NSUTF8StringEncoding];

            NSDictionary * resultFromJson =  [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingAllowFragments error:nil];

            

            NSArray * tempArray = resultFromJson[@"ws"];

            for (NSDictionary * tempDic in tempArray) {

                

                NSArray * cwArray = tempDic[@"cw"];

                for (NSDictionary * resultDic in cwArray) {

                    NSString * str = [NSString stringWithFormat:@"%@",resultDic[@"w"]];

                    [resultString appendString:str];

                }

            }

        }

        self.showLabel.text = [NSString stringWithFormat:@"%@",resultString];

    }

}

/*識別會話結束返回代理

 @ param error 錯誤碼,error.errorCode=0表示正常結束,非0表示發生錯誤。 */

- (void)onError: (IFlySpeechError *) error{

    NSLog(@"%@",error.errorDesc);

}

/**

 停止錄音回調

 ****/

- (void) onEndOfSpeech {

 

}

/**

 開始識別回調

 ****/

- (void) onBeginOfSpeech {

 

}

/**

 音量回呼函數 volume 0-30

 ****/

- (void) onVolumeChanged: (int)volume {

 

}

 

@end

 

2  文字變語音 語音合成播報部分

首先寫一個工具當然這個不是我寫的,能用^_^

.h 檔案如下

#import <UIKit/UIKit.h>

#import <AVFoundation/AVSpeechSynthesis.h>

 

@interface SpeechSynthesizer : NSObject

 

+ (instancetype)sharedSpeechSynthesizer;

 

- (void)speakString:(NSString *)string;

 

- (void)stopSpeak;

 

@end

.m 檔案如下

#import "SpeechSynthesizer.h"

 

@interface SpeechSynthesizer () <AVSpeechSynthesizerDelegate>

 

@property (nonatomic, strong, readwrite) AVSpeechSynthesizer *speechSynthesizer;

 

@end

 

@implementation SpeechSynthesizer

 

+ (instancetype)sharedSpeechSynthesizer

{

    static id sharedInstance = nil;

    

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        sharedInstance = [[SpeechSynthesizer alloc] init];

    });

    return sharedInstance;

}

 

- (instancetype)init

{

    if (self = [super init])

    {

        [self buildSpeechSynthesizer];

    }

    return self;

}

 

- (void)buildSpeechSynthesizer

{

    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0)

    {

        return;

    }

    

    self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];

    [self.speechSynthesizer setDelegate:self];

}

 

- (void)speakString:(NSString *)string

{

    if (self.speechSynthesizer)

    {

        AVSpeechUtterance *aUtterance = [AVSpeechUtterance speechUtteranceWithString:string];

        [aUtterance setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]];

        

        //iOS語音合成在iOS8及以下版本系統上語速異常

        if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)

        {

            aUtterance.rate = 0.25;//iOS7設定為0.25

        }

        else if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0)

        {

            aUtterance.rate = 0.15;//iOS8設定為0.15

        }

        

        if ([self.speechSynthesizer isSpeaking])

        {

            [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord];

        }

        

        [self.speechSynthesizer speakUtterance:aUtterance];

    }

}

- (void)stopSpeak

{

    if (self.speechSynthesizer)

    {

        [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];

    }

}

@end

使用方法如下

 

語音播報“世界那麼大我想去看看”

    [[SpeechSynthesizer sharedSpeechSynthesizer] speakString:@"世界那麼大我想去看看"];

停止播報

[[SpeechSynthesizer sharedSpeechSynthesizer] stopSpeak];

 

iOS語音辨識,語音播報,文字變語音播報,語音變文字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.