使用AVSpeechSynthesizer添加"文本转语音"的功能

AVSpeechSynthesizer播放一个或者多个语音内容:


#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
@interface TSSpeech: NSObject

@property(nonatomic, strong, readonly)AVSpeechSynthesizer *speechSynthesizer;

+ (instancetype)speechController;

- (void)beginConverstation;

@end

#import "TSSpeech.h"
@interface TSSpeech ()

@property(nonatomic, strong)AVSpeechSynthesizer *speechSynthesizer;
@property(nonatomic, strong)NSArray *speechVoices;
@property(nonatomic, strong)NSArray *speechStrings;


@end
@implementation TSSpeech
+ (instancetype)speechController{

   return  [[self alloc]init];

}

- (instancetype)init{

    self = [super init];
    if (self) {
        
        

        _speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
        _speechVoices      = @[
                         [AVSpeechSynthesisVoice voiceWithLanguage:@"zh_TW"],
                         [AVSpeechSynthesisVoice voiceWithLanguage:@"zh_HK"],
                         ];
        _speechStrings = [self buildingSpeechStrings];
        
        
    }
    return self;

}


- (NSArray *)buildingSpeechStrings{

    return @[
             @"你好世界",
             @"hello world",
             @"thank you very much",
             @"show me your code",
             ];

}
- (void)beginConverstation{


    for (NSUInteger i = 0; i < _speechStrings.count; i++) {
       
        AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:_speechStrings[i]];//创建新的 AVSpeechUtterance 实例
        utterance.voice = _speechVoices[i % 2];//交替使用不同的音色播放
        utterance.rate =0.4f;//设置播放语音的速率0.4f
        utterance.pitchMultiplier = 0.8f;//可在播放特定语句时改变声音的音调,pitchMultiplier介于0.5(低音)和2.0(高音)之间
        utterance.postUtteranceDelay  = 0.1f;//让语音播放合成器在播放下一句之前有短暂的暂停
        [_speechSynthesizer speakUtterance:utterance];//播放语音
        
        
    }
}
@end


以下内容为搜集的各国语言缩写列表


 th-TH  泰语 (泰国)
  pt-BR  葡萄牙语 (巴西)
  sk-SK  斯洛伐克语 (斯洛伐克)
  fr-CA  法语 (加拿大)
  ro-RO  罗马尼亚语 (罗马尼亚)
  no-NO  挪威语 (挪威)
  fi-FI  芬兰语 (芬兰)
  pl-PL  波兰语 (波兰)
  de-DE  德语 (德国)
  nl-NL  荷兰语 (荷兰)
  tr-TR  土耳其语 (土耳其)
  it-IT  意大利语 (意大利)
  pt-PT  葡萄牙语 (葡萄牙)
  fr-FR  法语 (法国)
  ru-RU  俄语 (俄罗斯)
  es-MX  西班牙语 (墨西哥) 
  zh-HK  汉语-香港
  sv-SE  瑞典语 (瑞典)
  hu-HU  匈牙利语 (匈牙利)
  zh-TW  汉语-台湾
  es-ES 西班牙语 (西班牙)
  zh-CN 汉语-普通话
  nl-BE 荷兰语 (比利时)
  en-GB 英语 (英国)
  ar-SA 阿拉伯语 (沙特阿拉伯)
  ko-KR 汉语
  cs-CZ 捷克语 (捷克共和国)
  en-ZA 英语 (南非)
  en-AU 英语 (澳大利亚)
  da-DK 丹麦语 (丹麦)
  en-US 英语 (美国)
  en-IE 英语 (爱尔兰)
  el-GR 希腊语 (希腊)
  ja-JP 日语


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值