美文网首页日常收录
iOS 文字转录音——AVSpeechSynthesizer的使

iOS 文字转录音——AVSpeechSynthesizer的使

作者: 曹波波 | 来源:发表于2018-01-25 10:22 被阅读110次
    iOS 文字转录音——AVSpeechSynthesizer的使用

    之前写了个语音转文字的,就再弄一个文字转语音的demo

    demo 开源地址:

    https://github.com/caobo56/CBWordToSound.git

    运行效果如下:

    WechatIMG268.png

    代码过程:

    1.创建项目

    2.添加AVFoundation Framework

    TARGET --》General -》Linked Frameworks and Libraries
    添加 AVFoundation
    并在项目中添加speech头文件

    #import <AVFoundation/AVFoundation.h>
    

    2.关键代码

    - (IBAction)soundAction:(id)send {
        UIButton * sender = (UIButton *)send;
        if(sender.selected == NO) {
            if([av isPaused]) {
                //如果暂停则恢复,会从暂停的地方继续
                [av continueSpeaking];
                sender.selected=!sender.selected;
            }else{
                //初始化对象
                av= [[AVSpeechSynthesizer alloc]init];
                av.delegate = self;//挂上代理
                AVSpeechUtterance*utterance = [[AVSpeechUtterance alloc]initWithString:_textView.text];//需要转换的文字
                utterance.rate=AVSpeechUtteranceMinimumSpeechRate;
                // 设置语速,范围0-1,注意0最慢,1最快;
                // AVSpeechUtteranceMinimumSpeechRate最慢,
                // AVSpeechUtteranceMaximumSpeechRate最快
                AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置发音,这是中文普通话
                utterance.voice= voice;
                [av speakUtterance:utterance];//开始
                sender.selected=!sender.selected;
            }
        }else{
    //        [av stopSpeakingAtBoundary:AVSpeechBoundaryWord];//感觉效果一样,对应代理>>>取消
            [av pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暂停
            sender.selected=!sender.selected;
        }
    }
    
    

    3.设置代理

    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{
        
        NSLog(@"---开始播放");
        
    }
    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
        
        NSLog(@"---完成播放");
        
    }
    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{
        
        NSLog(@"---播放中止");
        
    }
    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{
        
        NSLog(@"---恢复播放");
        
    }
    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{
        
        NSLog(@"---播放取消");
        
    }
    

    相关文章

      网友评论

        本文标题:iOS 文字转录音——AVSpeechSynthesizer的使

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