用系统的文字转语音功能
/*
专门用来保存单例代码
*/
// @interface
#define singleton_interface(className) \
+ (className *)shared##className;
// @implementation
#define singleton_implementation(className) \
static className *_instance; \
+ (id)allocWithZone:(NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
+ (className *)shared##className \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
}
XPAppSpeechTool.h
//
// XPAppSpeechTool.h
// XPAppBaseProject
//
// Created by LinXipin on 2021/12/7.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface XPAppSpeechTool : NSObject
singleton_interface(XPAppSpeechTool);
//设置音高,[0.5 - 2] 默认 = 1
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
/**utterance_pitchMultiplier*/
@property (assign, nonatomic) float utterance_pitchMultiplier;
//设置音量,[0-1] 默认 = 1
/**utterance_pitchMultiplier*/
@property (assign, nonatomic) float utterance_volume;
- (void)SpeechToolWithSpeechString:(NSString *)SpeechString;
@end
NS_ASSUME_NONNULL_END
XPAppSpeechTool.m
//
// XPAppSpeechTool.m
// XPAppBaseProject
//
// Created by LinXipin on 2021/12/7.
//
#import "XPAppSpeechTool.h"
#import <AVFoundation/AVSpeechSynthesis.h>
@interface XPAppSpeechTool ()<AVSpeechSynthesizerDelegate>
/**avSpeaker*/
@property (strong, nonatomic) AVSpeechSynthesizer *avSpeaker;
/**utterance*/
@property (strong, nonatomic) AVSpeechUtterance *utterance;
/**SpeechString*/
@property (copy, nonatomic) NSString *speechString;
@end
@implementation XPAppSpeechTool
+(instancetype)sharedXPAppSpeechTool
{
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc]init];
});
return sharedInstance;
}
- (void)SpeechToolWithSpeechString:(NSString *)SpeechString {
self.avSpeaker = nil;
self.utterance = nil;
self.speechString = SpeechString;
//开始朗读
[self.avSpeaker speakUtterance:self.utterance];
}
/**avSpeaker*/
- (AVSpeechSynthesizer *)avSpeaker
{
if (!_avSpeaker) {
//初始化语音合成器
_avSpeaker = [[AVSpeechSynthesizer alloc] init];
_avSpeaker.delegate = self;
}
return _avSpeaker;
}
/**utterance*/
- (AVSpeechUtterance *)utterance
{
if (!_utterance) {
_utterance = [[AVSpeechUtterance alloc] initWithString:self.speechString];
//设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
_utterance.rate = 0.5;
//设置音高,[0.5 - 2] 默认 = 1
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
_utterance.pitchMultiplier = self.utterance_pitchMultiplier?:1;
//设置音量,[0-1] 默认 = 1
_utterance.volume = self.utterance_volume?:1;
//读一段前的停顿时间
_utterance.preUtteranceDelay = 1;
//读完一段后的停顿时间
_utterance.postUtteranceDelay = 1;
//设置声音,是AVSpeechSynthesisVoice对象
//AVSpeechSynthesisVoice定义了一系列的声音, 主要是不同的语言和地区.
//voiceWithLanguage: 根据制定的语言, 获得一个声音.
//speechVoices: 获得当前设备支持的声音
//currentLanguageCode: 获得当前声音的语言字符串, 比如”ZH-cn”
//language: 获得当前的语言
//通过特定的语言获得声音
AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
//通过voicce标示获得声音
//AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithIdentifier:AVSpeechSynthesisVoiceIdentifierAlex];
_utterance.voice = voice;
}
return _utterance;
}
@end
使用
[[XPAppSpeechTool sharedXPAppSpeechTool] SpeechToolWithSpeechString:self.textView.text];
网友评论