123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- //
- // SDSoundPlayer.m
- // MapDemo
- //
- // Created by apple on 16/5/19.
- // Copyright © 2016年 danson. All rights reserved.
- #import "SDSoundPlayer.h"
- static SDSoundPlayer *soundplayer = nil;
- @interface SDSoundPlayer()<AVSpeechSynthesizerDelegate>
- {
- NSString *playString;
- }
- @property(nonatomic,strong)AVSpeechSynthesizer *avSpeech;//卧槽?不写成属性 代理不回调?
- @end
- @implementation SDSoundPlayer
- +(SDSoundPlayer *)SDSoundPlayerInit
- {
- if(soundplayer == nil)
- {
- soundplayer = [[SDSoundPlayer alloc]init];
- [soundplayer setDefaultWithVolume:-1.0 rate:-1.0 pitchMultiplier:-1.0];
- }
- return soundplayer;
- }
- //播放声音
- -(void)play:(NSString *)string
- {
- [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
- [[AVAudioSession sharedInstance] setActive:YES error:nil];
- NSLog(@"AVSpeechSynthesizer-%@。 string-%@",_avSpeech,string);
- if (_avSpeech && _avSpeech.speaking) {//&& ![string containsString:@"您已超速"] && ![playString containsString:@"拍照"] 这个@danson是给实操计时(辽宁没使用该功能)增加的一些限制,但是我@lee在写模拟计时的时候不需要这些限制。
- NSLog(@"zzStop;");
- [_avSpeech stopSpeakingAtBoundary:AVSpeechBoundaryWord];
- }
-
- playString = string;
-
- if(string && string.length > 0){
- if (!_avSpeech) {
- _avSpeech = [[AVSpeechSynthesizer alloc]init];
- _avSpeech.delegate = self;
- }
- AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:string];//设置语音内容
- utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言
- utterance.rate = self.rate; //设置语速
- utterance.volume = self.volume; //设置音量(0.0~1.0)默认为1.0
- utterance.pitchMultiplier = self.pitchMultiplier; //设置语调 (0.5-2.0)
- utterance.postUtteranceDelay = 1; //目的是让语音合成器播放下一语句前有短暂的暂停
-
- [_avSpeech speakUtterance:utterance];
- }
- }
- //初始化配置
- /**
- * 设置播放的声音参数 如果选择默认请传入 -1.0
- *
- * @param aVolume 音量(0.0~1.0)默认为1.0
- * @param aRate 语速(0.0~1.0)
- * @param aPitchMultiplier 语调 (0.5-2.0)
- */
- -(void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier
- {
- self.rate = aRate;
- self.volume = aVolume;
- self.pitchMultiplier = aPitchMultiplier;
-
- if (aRate == -1.0) {
- self.rate = 1;
- }
- if (aVolume == -1.0) {
- self.volume = 1;
- }
- if (aPitchMultiplier == -1.0) {
- self.pitchMultiplier = 1;
- }
- }
- - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
- {
- NSLog(@"didStart");
- }
- - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
- {
- NSLog(@"didFinish");
- }
- @end
|