美文网首页
iOS 集成语音播报

iOS 集成语音播报

作者: tongyuling | 来源:发表于2020-10-28 15:14 被阅读0次

    iOS7.0之后新添加了一些新的功能,里面就有系统自带的语音播报库, 需要 导入系统的AVFoundation 库

    #import <AVFoundation/AVFoundation.h>
    
    @interface ViewController ()<AVSpeechSynthesizerDelegate>
    
    /** 播报的内容 */
    @property (nonatomic, readwrite , strong) AVSpeechSynthesizer *synth;
    /** 负责播放 */
    @property (nonatomic, readwrite , strong) AVSpeechUtterance *utterance;
    
    @end
    

    基本使用

        NSString *str = @"支付宝 到账 100万 元";
        self.utterance = [AVSpeechUtterance speechUtteranceWithString:str];//成功集成语音播报
            
        //pitchMultiplier: 音高
        //
        //postUtteranceDelay: 读完一段后的停顿时间
        //
        //preUtteranceDelay: 读一段话之前的停顿
        //rate: 读地速度, 系统提供了三个速度: AVSpeechUtteranceMinimumSpeechRate, AVSpeechUtteranceMaximumSpeechRate, AVSpeechUtteranceDefaultSpeechRate
        self.utterance.rate = AVSpeechUtteranceDefaultSpeechRate;// 播报的语速
    
        //    中式发音
        AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
        //英式发音
    //    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-GB"];
    
        self.utterance.voice = voice;
    
        self.synth = [[AVSpeechSynthesizer alloc] init];
        self.synth.delegate = self;// 设置代理
        [self.synth speakUtterance:self.utterance];
    

    代理方法

    //已经开始
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14)) {
        
    }
    //已经说完
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14)) {
     
    }
    //已经暂停
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14)) {
     
    }
    //已经继续说话
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14)) {
     
    }
    //已经取消说话
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14)) {
     
    }
    //将要说某段话
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance API_AVAILABLE(ios(7.0), watchos(1.0), tvos(7.0), macos(10.14)) {
        
    }
    

    相关文章

      网友评论

          本文标题:iOS 集成语音播报

          本文链接:https://www.haomeiwen.com/subject/fazqvktx.html