zoukankan      html  css  js  c++  java
  • 初识--AVSpeechSynthesizer

    什么是AVSpeechSynchronizer?

    从IOS5开始,IOS系统已经在siri上集成了语音合成的功能,但是是私有API。但是在IOS7,新增了一个简单的API----AVSpeechSynthesizer来做这件事情。目前比较主流的语音识别和合成的工具“科大讯飞”很强大,使用起来也很简单,当然如果是简单的“文本到语音”的功能,在iOS中已经提供好了相关的API,开发者可以使用AV Foundation中的AVSpeechSynthesizer类来实现这个功能。

    那我就直接上代码喽:

    #import "ViewController.h"
    #import <AVFoundation/AVFoundation.h>
    
    @interface ViewController ()<AVSpeechSynthesizerDelegate>//AVSpeechSynthesizer代理
    
    //声明AVSpeechSynthesizer属性
    @property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //创建一个AVSpeechSynthesizer对象
        self.synthesizer = [[AVSpeechSynthesizer alloc]init];
        //设置代理人为当前控制器对象
        self.synthesizer.delegate = self;
        //创建AVSpeechUtterance对象(播放的语音内容都是通过实例化AVSpeechUtterance而得到)
        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:@"你好,就是爱你爱着你,你能把我咋地"];
        
        // 语速 0.0f~1.0f
        utterance.rate = 0.5f;
        // 声音的音调 0.5f~2.0f
        utterance.pitchMultiplier = 0.8f;
        // 使播放下一句的时候有0.1秒的延迟
        utterance.postUtteranceDelay = 0.1f;
        
        //系统默认是不支持中文的(下面是设置中文)
        utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//国家语言
        //播放
        [_synthesizer speakUtterance:utterance];
    }
    //AVSpeechSynthesizer的代理方法
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance{
        
        NSLog(@"开始播放");
    }
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance{
        
        NSLog(@"暂停播放");
    }
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance{
        NSLog(@"结束播放");
    }
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance{
        NSLog(@"退出播放状态");
    }
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance{
        NSLog(@"跳出播放状态");
    }
    -(void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance{
        NSLog(@"播放状态时,当前所播放的字符串范围,及AVSpeechUtterance实例(可通过此方法监听当前播放的字或者词");
    }
    
    @end
  • 相关阅读:
    fail-fast以及Iterator对象
    LeetCode~1351.统计有序矩阵中的负数
    LeetCode~75.颜色分类
    LeetCode~5364. 按既定顺序创建目标数组
    LeetCode~945.使数组唯一的最小增量
    LeetCode~409. 最长回文串
    笔记: SpringBoot + VUE实现数据字典展示功能
    JSON parse error: Cannot deserialize value of type `java.util.Date` from String
    为什么要用location的hash来传递参数?
    初识Git
  • 原文地址:https://www.cnblogs.com/LzwBlog/p/5688641.html
Copyright © 2011-2022 走看看