- 文本转语音
iOS提供了一个类AVSpeechSynthesizer来实现文本到语音的功能, 即读出文字
直接上代码:
AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
NSArray *speechStrings = @[@"Hello AV Foundation. How are you?",
@"我很好, 感谢!",
@"Are you excited about the book?",
@"是的, 我非常喜欢这本书.",
@"What's your favourite feature?",
@"我都很喜欢呢!",
@"It was great to speak with you!",
@"我也是, 祝你愉快"];
NSArray *voices = @[[AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"],
[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-cn"]];
for (int i = 0; i < speechStrings.count; i ++) {
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:speechStrings[i]];
utterance.voice = voices[i % 2]; // 语言
utterance.rate = 0.4f; // 播放语音的速度
utterance.pitchMultiplier = 0.8f; // 音调
utterance.postUtteranceDelay = 0.1f; // 下一句间隔时间
// 发音
[synthesizer speakUtterance:utterance];
}
- 设置音频会话类型
下面是常见音频会话分类
分类 | 作用 | 是否允许混音 | 音频输入 | 音频输出 |
---|---|---|---|---|
Ambient | 游戏, 效率应用程序 | V | V | |
Solo Ambient(默认) | 游戏, 效率应用程序 | V | ||
Playback | 音频和视频播放器 | 可选 | V | |
Record | 录音机, 音频捕捉 | V | ||
Play and Record | VOIP, 语音聊天 | 可选 | V | V |
Audio Processing | 离线会话和处理 | |||
Multi-Route | 使用外部硬件的高级A/V应用程序 | V | V |
设置音频会话
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
AVAudioSession *session = [AVAudioSession sharedInstance];
NSError *error;
if (![session setCategory:AVAudioSessionCategoryPlayback error:&error]) {
NSLog(@"Category Error: %@", [error localizedDescription]);
}
if (![session setActive:YES error:&error]) {
NSLog(@"Activation Error: %@", [error localizedDescription]);
}
return YES;
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有