SDSoundPlayer.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // SDSoundPlayer.m
  3. // MapDemo
  4. //
  5. // Created by apple on 16/5/19.
  6. // Copyright © 2016年 danson. All rights reserved.
  7. #import "SDSoundPlayer.h"
  8. static SDSoundPlayer *soundplayer = nil;
  9. @interface SDSoundPlayer()<AVSpeechSynthesizerDelegate>
  10. {
  11. NSString *playString;
  12. }
  13. @property(nonatomic,strong)AVSpeechSynthesizer *avSpeech;//卧槽?不写成属性 代理不回调?
  14. @end
  15. @implementation SDSoundPlayer
  16. +(SDSoundPlayer *)SDSoundPlayerInit
  17. {
  18. if(soundplayer == nil)
  19. {
  20. soundplayer = [[SDSoundPlayer alloc]init];
  21. [soundplayer setDefaultWithVolume:-1.0 rate:-1.0 pitchMultiplier:-1.0];
  22. }
  23. return soundplayer;
  24. }
  25. //播放声音
  26. -(void)play:(NSString *)string
  27. {
  28. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  29. [[AVAudioSession sharedInstance] setActive:YES error:nil];
  30. NSLog(@"AVSpeechSynthesizer-%@。 string-%@",_avSpeech,string);
  31. if (_avSpeech && _avSpeech.speaking) {//&& ![string containsString:@"您已超速"] && ![playString containsString:@"拍照"] 这个@danson是给实操计时(辽宁没使用该功能)增加的一些限制,但是我@lee在写模拟计时的时候不需要这些限制。
  32. NSLog(@"zzStop;");
  33. [_avSpeech stopSpeakingAtBoundary:AVSpeechBoundaryWord];
  34. }
  35. playString = string;
  36. if(string && string.length > 0){
  37. if (!_avSpeech) {
  38. _avSpeech = [[AVSpeechSynthesizer alloc]init];
  39. _avSpeech.delegate = self;
  40. }
  41. AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc]initWithString:string];//设置语音内容
  42. utterance.voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];//设置语言
  43. utterance.rate = self.rate; //设置语速
  44. utterance.volume = self.volume; //设置音量(0.0~1.0)默认为1.0
  45. utterance.pitchMultiplier = self.pitchMultiplier; //设置语调 (0.5-2.0)
  46. utterance.postUtteranceDelay = 1; //目的是让语音合成器播放下一语句前有短暂的暂停
  47. [_avSpeech speakUtterance:utterance];
  48. }
  49. }
  50. //初始化配置
  51. /**
  52. * 设置播放的声音参数 如果选择默认请传入 -1.0
  53. *
  54. * @param aVolume 音量(0.0~1.0)默认为1.0
  55. * @param aRate 语速(0.0~1.0)
  56. * @param aPitchMultiplier 语调 (0.5-2.0)
  57. */
  58. -(void)setDefaultWithVolume:(float)aVolume rate:(CGFloat)aRate pitchMultiplier:(CGFloat)aPitchMultiplier
  59. {
  60. self.rate = aRate;
  61. self.volume = aVolume;
  62. self.pitchMultiplier = aPitchMultiplier;
  63. if (aRate == -1.0) {
  64. self.rate = 1;
  65. }
  66. if (aVolume == -1.0) {
  67. self.volume = 1;
  68. }
  69. if (aPitchMultiplier == -1.0) {
  70. self.pitchMultiplier = 1;
  71. }
  72. }
  73. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance
  74. {
  75. NSLog(@"didStart");
  76. }
  77. - (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance
  78. {
  79. NSLog(@"didFinish");
  80. }
  81. @end