前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS-文本转语音

iOS-文本转语音

作者头像
周希
发布2019-10-15 01:42:57
7260
发布2019-10-15 01:42:57
举报
文章被收录于专栏:APP自动化测试

- 文本转语音

iOS提供了一个类AVSpeechSynthesizer来实现文本到语音的功能, 即读出文字

直接上代码:

代码语言:javascript
复制
    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

设置音频会话

代码语言:javascript
复制
- (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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档