美文网首页
iOS 读取txt文本 、语音播报文本

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

作者: ShanJiJi | 来源:发表于2017-08-22 11:08 被阅读107次

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

    
    #import "ViewController.h"
    #import <AVFoundation/AVSpeechSynthesis.h>
    
    @interface ViewController ()<AVSpeechSynthesizerDelegate>
    
    // 播放暂停按钮  xib
    @property (weak, nonatomic) IBOutlet UIButton *startBtn;
    // 语音合成器
    @property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
       [super viewDidLoad];
       
       [self.startBtn setTitle:@"开始" forState:0];
       [self.startBtn setTitle:@"暂停" forState:UIControlStateSelected];
       
    }
    
    - (NSString *)getTxtFileContent {
       // 获取txt文件中的文本内容
       NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyTestFile" ofType:@"txt"];
       NSString *text = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
       return text;
    }
    
    // 朗读文本内容方法
    - (void)readingContentText:(NSString *)text {
       
       if (!self.synthesizer) {
           self.synthesizer = [[AVSpeechSynthesizer alloc] init];
           self.synthesizer.delegate = self;
       }
       
       // 将需要朗读的文本合成语音
       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"
        */
       // 这里合成语音的语言 中文
       AVSpeechSynthesisVoice *voicLanguage = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
       utterance.voice = voicLanguage;
       // 语速 0.0f ~ 1.0f
       utterance.rate = 0.5f;
       // 声音的音调 0.5f ~ 2.0f
       utterance.pitchMultiplier = 1.0f;
       // 使播放下一句的时候有0.1秒的延迟
       utterance.postUtteranceDelay = 0.1f;
       // 设置音量大小 0.0f ~ 1.0f
       utterance.volume = 0.1f;
       
       // 播放合成语音
       [self.synthesizer speakUtterance:utterance];
       
       
    }
    
    
    // 开始 暂停按钮事件
    - (IBAction)startButtonAction:(UIButton *)button {
       
       if (button.selected) {
           
           // 暂停
           [self.startBtn setTitle:@"开始" forState:0];
           
           // 判断是否播放中,如果播放中则暂停播放
           if ([self.synthesizer isSpeaking]) {
               [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryWord];
           }
       }
       else {
           
           // 开始
           [self.startBtn setTitle:@"暂停" forState:UIControlStateSelected];
           
           // 判断是否暂停播放
           if ([self.synthesizer isPaused]) {
               [self.synthesizer continueSpeaking];
           }
           else {
               // 如果没有暂停,则重新创建新的播放语音并且开始播放
               NSString *text = [self getTxtFileContent];
               [self readingContentText:text];
           }
       }
       
       button.selected = !button.selected;
       
    }
    
    #pragma mark - AVSpeechSynthesizerDelegate
    
    // 开始播放
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
       NSLog(@"开始播放");
    }
    - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
       NSLog(@"完成播放");
       self.startBtn.selected = !self.startBtn.selected;
    }
    - (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(@"将要播放某一段语音");
    }
    
    
    

    相关文章

      网友评论

          本文标题:iOS 读取txt文本 、语音播报文本

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