标签:voice 声音 mon 支付宝 全局 atom 时间 台湾 void
因新项目需求,需要接入类似支付宝收款提示声----“支付宝到账xxxx元”,就查看了一些文章,苹果还是想的很周全,废话不多说。
在General -> Linked Frameworks and Libraries 中导入 AVFoundation框架
头文件包含 #import <AVFoundation/AVSpeechSynthesis.h>
定义一个全局变量(局部也可),如果需要进行暂停,继续播放等控制操作,定义全局变量
@property(nonatomic,strong) AVSpeechSynthesizer *voice;
.m下创建一个textfield --------- 用来输入需要转换的文字 (如果不需要就不创建,直接AVSpeechUtterance初始化的时候添加文字即可 稍后会讲到)
创建一个按钮 ---------用来控制语音的暂停/继续。(按钮创建就不用多说了,不会的赶紧买块豆腐去O(∩_∩)O)
按钮点击方法 在实现中需要将按钮传过去
[speechaddTarget:selfaction:@selector(speech:)forControlEvents:UIControlEventTouchUpInside];
实现
- (void)speech:(UIButton*)btn
因为要用这个按钮来判断点击状态 让她播放或者暂停或者继续播放
在实现方法中,进行初始化。
如果需要用代理方法控制声音的暂停/继续那就需要设置代理,不需要就请忽略
先初始化 后面带上需要转换的文字,也就是textField输入的内容。
接着 我们就可以玩一玩这个语音了
第一个 pitchMultiplier
音调调节 调节范围 :[0.5 - 2] 默认是:1
第二个 volume
音量调节 调节范围 :[0-1] 默认是 :1
第三个 tate
语速调节 调节范围 :[0-1] 0最慢 1 最快
说个题外话 之前一直认为支付宝的提示声也是转出来的,后来看到一个视频,发现我错了, 支付宝的提示声 是他们一个员工的声音录制的,这个员工是台湾人,也是支付宝的交互设计师,叫唐啥来着 我记不起来了。日后有机会 可以封装一个 定制文字转语音播放的库,各式各样的声音 。。。。扯多了 继续,
"[AVSpeechSynthesisVoice 0x978a0b0] Language: th-TH",
"[AVSpeechSynthesisVoice 0x977a450] Language: pt-BR",
"[AVSpeechSynthesisVoice 0x977a480] Language: sk-SK",
"[AVSpeechSynthesisVoice 0x978ad50] Language: fr-CA",
"[AVSpeechSynthesisVoice 0x978ada0] Language: ro-RO",
"[AVSpeechSynthesisVoice 0x97823f0] Language: no-NO",
"[AVSpeechSynthesisVoice 0x978e7b0] Language: fi-FI",
"[AVSpeechSynthesisVoice 0x978af50] Language: pl-PL",
"[AVSpeechSynthesisVoice 0x978afa0] Language: de-DE",
"[AVSpeechSynthesisVoice 0x978e390] Language: nl-NL",
"[AVSpeechSynthesisVoice 0x978b030] Language: id-ID",
"[AVSpeechSynthesisVoice 0x978b080] Language: tr-TR",
"[AVSpeechSynthesisVoice 0x978b0d0] Language: it-IT",
"[AVSpeechSynthesisVoice 0x978b120] Language: pt-PT",
"[AVSpeechSynthesisVoice 0x978b170] Language: fr-FR",
"[AVSpeechSynthesisVoice 0x978b1c0] Language: ru-RU",
"[AVSpeechSynthesisVoice 0x978b210] Language: es-MX",
"[AVSpeechSynthesisVoice 0x978b2d0] Language: zh-HK",中文(香港)粤语
"[AVSpeechSynthesisVoice 0x978b320] Language: sv-SE",
"[AVSpeechSynthesisVoice 0x978b010] Language: hu-HU",
"[AVSpeechSynthesisVoice 0x978b440] Language: zh-TW",中文(台湾)
"[AVSpeechSynthesisVoice 0x978b490] Language: es-ES",
"[AVSpeechSynthesisVoice 0x978b4e0] Language: zh-CN",中文(普通话)
"[AVSpeechSynthesisVoice 0x978b530] Language: nl-BE",
"[AVSpeechSynthesisVoice 0x978b580] Language: en-GB",英语(英国)
"[AVSpeechSynthesisVoice 0x978b5d0] Language: ar-SA",
"[AVSpeechSynthesisVoice 0x978b620] Language: ko-KR",
"[AVSpeechSynthesisVoice 0x978b670] Language: cs-CZ",
"[AVSpeechSynthesisVoice 0x978b6c0] Language: en-ZA",
"[AVSpeechSynthesisVoice 0x978aed0] Language: en-AU",
"[AVSpeechSynthesisVoice 0x978af20] Language: da-DK",
"[AVSpeechSynthesisVoice 0x978b810] Language: en-US",英语(美国)
"[AVSpeechSynthesisVoice 0x978b860] Language: en-IE",
"[AVSpeechSynthesisVoice 0x978b8b0] Language: hi-IN",
"[AVSpeechSynthesisVoice 0x978b900] Language: el-GR",
"[AVSpeechSynthesisVoice 0x978b950] Language: ja-JP"
[voice speakUtterance:speech]; //开始播放
[voice continueSpeaking]; // 停止播放
[voice pauseSpeakingAtBoundary:AVSpeechBoundaryWord];//暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance;
//开始
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance;
//完成
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance;
//暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance;
//恢复
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance;
//取消
这是我git地址 对这些简单的进行了包装 大家有空可以去看看https://github.com/FormerMonster/JCVoice 纯属为了方便设置 ,节省开发时间。大家见谅 如若有需要修正的 望指正 。
标签:voice 声音 mon 支付宝 全局 atom 时间 台湾 void
原文地址:https://www.cnblogs.com/deepsum/p/9713526.html