1、导入SDK
OC支持版本
pod 'MicrosoftCognitiveServicesSpeech-iOS', '~> 1.33.0'
2、导入头文件
import <MicrosoftCognitiveServicesSpeech/SPXSpeechApi.h>
3、初始化
从自己服务器或者微软开发后台自己拿到ttsRegion和ttsToken
SPXSpeechConfiguration * config = [[SPXSpeechConfiguration alloc] initWithAuthorizationToken:@"ttsToken"] region:[@"ttsRegion"] error:nil];
4、转化
/**
1、设置语音文件保存地址
*/
NSString *uuid = [[NSUUID UUID] UUIDString];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *path = [documentDirectory stringByAppendingPathComponent:@"Voice"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
path = [NSString stringWithFormat:@"%@/%@.wav",path,uuid];
/**
2、配置地址
*/
SPXAudioConfiguration * audioConfig = [[SPXAudioConfiguration alloc] initWithWavFileOutput:path];
SPXSpeechSynthesizer * synthesize = [[SPXSpeechSynthesizer alloc] initWithSpeechConfiguration:config audioConfiguration:audioConfig];
/**
3、
配置角色、人物、风格等参数,并开始转化我使用的是ssml方式,
结果监听、结果描述
还有其他方式,可在官方文档查阅
*/
NSString * ssmlString = [TTVConfigService.shareService ssmlString:self.typeTextView.text];
SPXSpeechSynthesisResult *result = [synthesize speakSsml:ssmlString];
NSLog(@"result %.f",result.audioDuration);
SPXSpeechSynthesisCancellationDetails * detail = [[SPXSpeechSynthesisCancellationDetails alloc] initFromCanceledSynthesisResult:result];
/**
替换掉对应位置的参数。
MainJson:是在官方下载的支持的所有语言和角色、风格等。
这里只简单设置了三个参数,还有很多可根据自己需求设置
*/
- (NSString *) ssmlString:(NSString *) content {
NSString * path = [[NSBundle mainBundle] pathForResource:@"MainJson" ofType:@"geojson"];
if (!path) {
return nil;
}
NSString *parseJason = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSData *jaonData = [parseJason dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jaonData options:NSJSONReadingMutableContainers error:nil];
__block NSString * language = SafeString(self.language);
NSArray * lanuagesArray = self.totalConfigDictionary[@"voiceVoList"];
[lanuagesArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj_, NSUInteger idx, BOOL * _Nonnull stop) {
if ([[obj_ objectForKey:@"language"] isEqualToString:self.language]) {
language = [obj_ objectForKey:@"locale"];
}
}];
NSString * ssmlString = [NSString stringWithFormat:dict[@"mainJson"],SafeString(language) ,SafeString(self.lanuageShortName),SafeString(self.selectedVoice),SafeString(self.selectedStyle),content];
NSLog(@"%@",ssmlString);
return ssmlString;
}
{
"mainJson" : "<speak xmlns:mstts=\"https://www.w3.org/2001/mstts\" version=\"1.0\"\n xmlns=\"http://www.w3.org/2001/10/synthesis\" xml:lang=\"%@\">\n <voice name=\"%@\">\n <mstts:express-as style=\"%@\" role=\"%@\" styledegree=\"1\">\n %@\n </mstts:express-as>\n </voice>\n</speak>\n\n"
}
然后就可以将本地语音文件使用到自己的业务了。