美文网首页
iOS-系统语音播报.txt文本

iOS-系统语音播报.txt文本

作者: 329fd8af610c | 来源:发表于2019-12-30 13:22 被阅读0次

本篇记录一下系统播放文本的功能。主要以代码+注释为展示方式。

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<AVSpeechSynthesizerDelegate>
@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
@property (nonatomic, strong) UIButton *button1;
@property (nonatomic, strong) UIButton *button2;
@property (nonatomic, strong) UILabel *label;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, 400, 100)];
    self.label.numberOfLines = 0;
    self.label.textColor = [UIColor redColor];
    self.label.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.label];
    
    self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button1 addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
    self.button1.backgroundColor = [UIColor redColor];
    [self.button1 setTitle:@"开始播放" forState:UIControlStateNormal];
    self.button1.frame = CGRectMake(100, 200, 100, 100);
    [self.view addSubview:self.button1];
    
    
    self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button2 addTarget:self action:@selector(endPlay:) forControlEvents:UIControlEventTouchUpInside];
    self.button2.backgroundColor = [UIColor purpleColor];
    [self.button2 setTitle:@"结束播放" forState:UIControlStateNormal];
    self.button2.frame = CGRectMake(100, 300, 100, 100);
    [self.view addSubview:self.button2];
    
    [self getTxtFileContent];
    // Do any additional setup after loading the view.
}

⚠️:下面才是系统播放文本的重点代码

- (NSString *)getTxtFileContent {
    // 获取txt文件中的文本内容
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"demo" ofType:@"txt"];
    NSString *text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    return text;
}
- (void)startPlay:(UIButton *)sender {
    NSString *text = [self getTxtFileContent];
    [self readingContentText:text];
    
}
- (void)endPlay:(UIButton *)sender {
    
    if ([self.synthesizer isSpeaking]) {
        [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
    }
}
- (void)readingContentText:(NSString *)text {
    1.初始化播音器
    if (!self.synthesizer) {
        self.synthesizer = [[AVSpeechSynthesizer alloc]init];
        self.synthesizer.delegate = self;
    }
    2.将需要朗读的文本合成语音
    AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:text];
    
    /*
     avspeech支持的语言种类包括:
     "th-TH","pt-BR","sk-SK","fr-CA","ro-RO","no-NO","fi-FI","pl-PL","de-DE",
     "nl-NL","id-ID","tr-TR","it-IT","pt-PT","fr-FR","ru-RU","es-MX","zh-HK",
     中文(香港)粤语"sv-SE","hu-HU","zh-TW",中文(台湾)"es-ES","zh-CN",中文(普通话)
     "nl-BE","en-GB",英语(英国)"ar-SA","ko-KR","cs-CZ","en-ZA","en-AU","da-DK",
     "en-US",英语(美国)"en-IE","hi-IN","el-GR","ja-JP"
     */
    
    3.播放中文
    AVSpeechSynthesisVoice *voiceLanguage = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    utterance.voice = voiceLanguage;
 
    4.语速0.0f - 1.0f
    utterance.rate = 0.5f;
    
    5.播放音调 0.5f-2.0f
    utterance.pitchMultiplier = 1.0f;
    
    6.播放下一句的时候有 0.2s 延迟时间
    utterance.postUtteranceDelay = 0.2f;
    
    7.设置音量大小  0.0f-1.0f
    utterance.volume = 0.1f;
    
    8.播放合成语音
    [self.synthesizer speakUtterance:utterance];
    
   9.暂停播放合成语音
//    [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
    
    
}


我们可以根据AVSpeechSynthesizerDelegate坚挺播放状态

#pragma mark - AVSpeechSynthesizerDelegate

// 开始播放
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
   NSLog(@"开始播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
   NSLog(@"完成播放");
   
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
   NSLog(@"暂停播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
   NSLog(@"恢复播放");
}
- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
   NSLog(@"取消播放");
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
   NSLog(@"将要播放某一段语音");
    self.label.text = utterance.speechString;
    
}

这个功能以代码+注释为主。
demo地址:https://github.com/sisios/systemSpeechTextDemo.git

先记录这些,感谢阅读,如有错误,不吝赐教!

相关文章

  • iOS-系统语音播报.txt文本

    本篇记录一下系统播放文本的功能。主要以代码+注释为展示方式。 ⚠️:下面才是系统播放文本的重点代码 我们可以根据A...

  • iOS 读取txt文本 、语音播报文本

    获取项目中对应txt文件的内容,并且将获得到的文本转成语音播报

  • 文本语音播报

    不用讯飞语音的原因就是离线语音有点贵。如果感觉下面方法不够优雅,并且就播放几个固定的语音那就可以使用系统播放音频文...

  • Xamarin Essentials教程语音播报TextToSp

    Xamarin Essentials教程语音播报TextToSpeech 语音播报是一种将文本信息转化为音频信息的...

  • 功能博客整理

    1.类似支付宝后台语音播报 ``` http://lumengru.com/2017/06/23/iOS-%E5%...

  • TTS

    · TTS · Text To Speech · 文本转语音 · 语音播报类 · 生成播放的内容 · 实现播放 ·...

  • 语音播报

    项目中用到了语音播报推送内容,发现iOS7以后系统支持语音播报.记下用以备用

  • 斑马听书APP

    开发出新版的斑马听书APP 支持TTS文本转语音 可以听TXT文本阅读 需要手机支持语音播放 功能:有4个主要页面...

  • java实现自动朗读txt文本中的内容

    实现一个java程序读取txt文本中的内容,并且调用系统语音来朗读出来。 1.准备工作 Jacob下载地址http...

  • iOS 系统语音播报

    将一段文字转化为iOS系统语音播放 VoiceManager.h VoiceManager.m

网友评论

      本文标题:iOS-系统语音播报.txt文本

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