• 苹果原生文字转语音播报


    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
    
  • 相关阅读:
    面向对象串讲
    昨日回顾
    socketserver模块
    今日总结
    在centos6.5-64bit上安装wxHexEditor,以查看编译二进制文件
    spring security 关于 http.sessionManagement().maximumSessions(1);的探究
    spring boot + spring security +前后端分离【跨域】配置 + ajax的json传输数据
    window10 查看端口列表
    spring boot 解决 跨域 的两种方法 -- 前后端分离
    spring security 动态 修改当前登录用户的 权限
  • 原文地址:https://www.cnblogs.com/CH520/p/10087593.html
Copyright © 2020-2023  润新知