码迷,mamicode.com
首页 > 移动开发 > 详细

苹果原生文字转语音播报

时间:2018-12-08 15:37:43      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:Speak   通话   select   string   最快   @param   nsobject   imp   delay   

1、CHiOSSpeech.h

//
// 文 件 名:CHiOSSpeech.h
// 
// 版权所有:Copyright ? 2018年 leLight. All rights reserved.
// 创 建 者:leLight 
// 创建日期:2018/7/30.
// 文档说明:苹果原生文字转语音播报.
// 修 改 人:
// 修改日期:
// 

#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>

@protocol CHiOSSpeechDelegate <NSObject>
@optional
/************ 开始播放 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance;
/************ 完成播放 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance;
/************ 播放中止 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance;
/************ 恢复播放 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance;
/************ 播放取消 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance;

@end

@interface CHiOSSpeech : NSObject

/** 文字播报代理 */
@property (nonatomic, weak) id<CHiOSSpeechDelegate> delegate;

/************ 单例对象 *****************************/
+ (CHiOSSpeech *)sharedInstance;
/************ 开始播放 *****************************/
- (void)startSpeekWithString:(NSString *)string;
/************ 暂停播放 *****************************/
- (void)pauseSpeaking;
/************ 继续播放 *****************************/
- (void)continueSpeaking;

// 初始化配置:这里不设置则使用默认参数
/**
 *  设置播放的声音参数 如果选择默认请传入 -1.0
 *
 *  @param aVolume          音量(0.0~1.0)默认为1.0
 *  @param aRate            语速(0.0~1.0)默认为1.0
 *  @param aPitchMultiplier 语调 (0.5-2.0)默认为1.0
  *  @param languageCode    语言          默认为 中文普通话:@"zh-CN"
 */
- (void)setDefaultWithVolume:(float)aVolume
                        rate:(CGFloat)aRate
             pitchMultiplier:(CGFloat)aPitchMultiplier
                languageCode:(NSString *)languageCode;

@end

2、CHiOSSpeech.m

//
// 文 件 名:CHiOSSpeech.m
// 
// 版权所有:Copyright ? 2018年 leLight. All rights reserved.
// 创 建 者:leLight 
// 创建日期:2018/7/30.
// 文档说明:苹果原生文字转语音播报.
// 修 改 人:
// 修改日期:
// 

#import "CHiOSSpeech.h"

@interface CHiOSSpeech () <AVSpeechSynthesizerDelegate>
{
    AVSpeechSynthesizer      *av;
    AVSpeechUtterance        *utterance;
}

/** 语速 */
@property(nonatomic, assign) float rate;
/** 音量 */
@property(nonatomic, assign) float volume;
/** 音调 */
@property(nonatomic, assign) float pitchMultiplier;
/** 音调 */
@property(nonatomic, copy) NSString *languageCode;

@end

@implementation CHiOSSpeech

/************ 单例对象 *****************************/
+ (CHiOSSpeech *)sharedInstance {
    static CHiOSSpeech *sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedClient = [[CHiOSSpeech alloc] init];
    });
    return sharedClient;
}

/************ 初始化创建 *****************************/
- (instancetype)init {
    self = [super init];
    if (self) {
        // 初始化对象
        av = [[AVSpeechSynthesizer alloc] init];
        // 挂上代理
        av.delegate = self;
        // 初始化配置
        [self setDefaultWithVolume:-1.0 rate:-1.0 pitchMultiplier:-1.0 languageCode:@"zh-CN"];
        
    };
    return self;
}

/************ 开始播放 *****************************/
- (void)startSpeekWithString:(NSString *)string {
    
    utterance = [[AVSpeechUtterance alloc] initWithString:string];
    // 设置语速,范围0-1,注意0最慢,1最快;AVSpeechUtteranceMinimumSpeechRate最慢,AVSpeechUtteranceMaximumSpeechRate最快
    utterance.rate = 0.5;
    //设置发音,这是中文普通话:@"zh-CN"
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:self.languageCode];
    utterance.voice = voice;
    // 设置语速
    utterance.rate = self.rate;
    // 设置音量(0.0~1.0)默认为1.0
    utterance.volume = self.volume;
    // 设置语调 (0.5-2.0)
    utterance.pitchMultiplier = self.pitchMultiplier;
    // 目的是让语音合成器播放下一语句前有短暂的暂停
    utterance.postUtteranceDelay = 1;
    
    // 开始
    [av speakUtterance:utterance];
}

/************ 暂停播放 *****************************/
- (void)pauseSpeaking {
    [av pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
}

/************ 继续播放 *****************************/
- (void)continueSpeaking {
    // 如果暂停则恢复,会从暂停的地方继续
    [av continueSpeaking];
}

#pragma mark ***************************** AVSpeechSynthesizerDelegate ***********************************************
/************ 开始播放 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance {
    
    CHLog(@"是否正在播放:%d", synthesizer.isSpeaking);
    CHLog(@"是否处于播放:%d", synthesizer.isPaused);
    CHLog(@"播放的内容:%@", utterance.speechString);
    
    CHLog(@"---开始播放");
    
    if([self.delegate respondsToSelector:@selector(speechSynthesizer: didStartSpeechUtterance:)]) {
        [self.delegate speechSynthesizer:synthesizer didStartSpeechUtterance:utterance];
    }
}

/************ 完成播放 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance {
    CHLog(@"---完成播放");
    
    if([self.delegate respondsToSelector:@selector(speechSynthesizer: didFinishSpeechUtterance:)]) {
        [self.delegate speechSynthesizer:synthesizer didFinishSpeechUtterance:utterance];
    }
}

/************ 播放中止 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance {
    CHLog(@"---播放中止");
    
    if([self.delegate respondsToSelector:@selector(speechSynthesizer: didPauseSpeechUtterance:)]) {
        [self.delegate speechSynthesizer:synthesizer didPauseSpeechUtterance:utterance];
    }
}

/************ 恢复播放 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance {
    CHLog(@"---恢复播放");
    
    if([self.delegate respondsToSelector:@selector(speechSynthesizer: didContinueSpeechUtterance:)]) {
        [self.delegate speechSynthesizer:synthesizer didContinueSpeechUtterance:utterance];
    }
}

/************ 播放取消 *****************************/
- (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance*)utterance {
    CHLog(@"---播放取消");
    
    if([self.delegate respondsToSelector:@selector(speechSynthesizer: didCancelSpeechUtterance:)]) {
        [self.delegate speechSynthesizer:synthesizer didCancelSpeechUtterance:utterance];
    }
}

// 初始化配置
/**
 *  设置播放的声音参数 如果选择默认请传入 -1.0
 *
 *  @param aVolume          音量(0.0~1.0)默认为1.0
 *  @param aRate            语速(0.0~1.0)默认为1.0
 *  @param aPitchMultiplier 语调 (0.5-2.0)默认为1.0
 *  @param languageCode     语言          默认为 中文普通话:@"zh-CN"
 */
- (void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier languageCode:(NSString *)languageCode {
    
    self.rate   = aRate;
    self.volume = aVolume;
    self.pitchMultiplier = aPitchMultiplier;
    self.languageCode = languageCode;
    
    if (aRate == -1.0) {
        self.rate = 0.5;
    }
    if (aVolume == -1.0) {
        self.volume = 1.0;
    }
    if (aPitchMultiplier == -1.0) {
        self.pitchMultiplier = 1;
    }
}

@end

苹果原生文字转语音播报

标签:Speak   通话   select   string   最快   @param   nsobject   imp   delay   

原文地址:https://www.cnblogs.com/CH520/p/10087593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!