1 Star 1 Fork 1

maoxm/iOS_App_Template

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SpeechUtils.m 3.10 KB
一键复制 编辑 原始数据 按行查看 历史
maoxm 提交于 3年前 . 添加注释
//
// SpeechUtils.m
// iOS_App_Template
//
// Created by mac on 2021/1/6.
// Copyright © 2022 mxm. All rights reserved.
//
#import "SpeechUtils.h"
#import <AVFoundation/AVSpeechSynthesis.h>
@interface SpeechUtils (Delegate) <AVSpeechSynthesizerDelegate>
@end
//要在 TARGETS -> General 加入 AVFoundation 依赖库
@implementation SpeechUtils
{
AVSpeechSynthesizer *_avSpeaker;
}
- (void)player
{
//初始化语音合成器
_avSpeaker = [[AVSpeechSynthesizer alloc] init];
_avSpeaker.delegate = self;
//初始化要说出的内容
AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:@"6 6 6"];
//设置语速,语速介于AVSpeechUtteranceMaximumSpeechRate和AVSpeechUtteranceMinimumSpeechRate之间
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
utterance.rate = 0.5;
//设置音高,[0.5 - 2] 默认 = 1
//AVSpeechUtteranceMaximumSpeechRate
//AVSpeechUtteranceMinimumSpeechRate
//AVSpeechUtteranceDefaultSpeechRate
utterance.pitchMultiplier = 1;
//设置音量,[0-1] 默认 = 1
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;
//开始朗读
[_avSpeaker speakUtterance:utterance];
}
#pragma mark - AVSpeechSynthesizerDelegate
//已经开始
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
{
}
//已经说完
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
{
_avSpeaker = nil;
//如果朗读要循环朗读,可以在这里再次调用朗读方法
//[_avSpeaker speakUtterance:utterance];
}
//已经暂停
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance
{
}
//已经继续说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance
{
}
//已经取消说话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance
{
}
//将要说某段话
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance
{
}
@end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Objective-C
1
https://gitee.com/maoxm/iOS_App_Template.git
git@gitee.com:maoxm/iOS_App_Template.git
maoxm
iOS_App_Template
iOS_App_Template
master

搜索帮助