DKAudioPlayerHelper.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // DKAudioPlayerHelper.m
  3. // MessageDisplayKit
  4. //
  5. // Created by Aevitx on 14-1-22.
  6. // Copyright (c) 2014年 Aevitx. All rights reserved.
  7. //
  8. #import "DKAudioPlayerHelper.h"
  9. #import "DKVoiceCommonHelper.h"
  10. @implementation DKAudioPlayerHelper
  11. #pragma mark - Public Methed
  12. - (void)managerAudioWithFileName:(NSString*)amrName toPlay:(BOOL)toPlay {
  13. if (toPlay) {
  14. [self playAudioWithFileName:amrName];
  15. } else {
  16. [self pausePlayingAudio];
  17. }
  18. }
  19. //暂停
  20. - (void)pausePlayingAudio {
  21. if (_player) {
  22. [_player pause];
  23. if ([self.delegate respondsToSelector:@selector(didAudioPlayerPausePlay:)]) {
  24. [self.delegate didAudioPlayerPausePlay:_player];
  25. }
  26. }
  27. }
  28. - (void)stopAudio {
  29. [self setPlayingFileName:@""];
  30. [self setPlayingIndexPathInFeedList:nil];
  31. if (_player && _player.isPlaying) {
  32. [_player stop];
  33. }
  34. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
  35. if ([self.delegate respondsToSelector:@selector(didAudioPlayerStopPlay:)]) {
  36. [self.delegate didAudioPlayerStopPlay:_player];
  37. }
  38. }
  39. #pragma mark - action
  40. //播放转换后wav
  41. - (void)playAudioWithFileName:(NSString*)fileName {
  42. if (fileName.length > 0) {
  43. //不随着静音键和屏幕关闭而静音。code by Aevit
  44. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  45. if (_playingFileName && [fileName isEqualToString:_playingFileName]) {//上次播放的录音
  46. if (_player) {
  47. [_player play];
  48. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
  49. if ([self.delegate respondsToSelector:@selector(didAudioPlayerBeginPlay:)]) {
  50. [self.delegate didAudioPlayerBeginPlay:_player];
  51. }
  52. }
  53. } else {//不是上次播放的录音
  54. if (_player) {
  55. [_player stop];
  56. self.player = nil;
  57. }
  58. /*
  59. [self convertAmrToWav:amrName];
  60. NSString *wavName = [amrName stringByReplacingOccurrencesOfString:@"wavToAmr" withString:@"amrToWav"];
  61. AVAudioPlayer *pl = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[DKVoiceCommonHelper getPathByFileName:fileName ofType:@"wav"]] error:nil];
  62. */
  63. AVAudioPlayer *pl = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:fileName] error:nil];
  64. pl.delegate = self;
  65. [pl play];
  66. self.player = pl;
  67. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
  68. if ([self.delegate respondsToSelector:@selector(didAudioPlayerBeginPlay:)]) {
  69. [self.delegate didAudioPlayerBeginPlay:_player];
  70. }
  71. }
  72. self.playingFileName = fileName;
  73. }
  74. }
  75. /*
  76. #pragma mark - amr转wav
  77. - (void)convertAmrToWav:(NSString*)amrName {
  78. if (amrName.length > 0){
  79. NSString *wavName = [amrName stringByReplacingOccurrencesOfString:@"wavToAmr" withString:@"amrToWav"];// [amrName stringByAppendingString:@"amrToWav"];
  80. //转格式
  81. [VoiceConverter amrToWav:[SCAudioRecordManager getPathByFileName:amrName ofType:@"amr"] wavSavePath:[SCAudioRecordManager getPathByFileName:wavName ofType:@"wav"]];
  82. }
  83. }
  84. */
  85. #pragma mark - Getter
  86. - (AVAudioPlayer*)player {
  87. return _player;
  88. }
  89. - (BOOL)isPlaying {
  90. if (!_player) {
  91. return NO;
  92. }
  93. return _player.isPlaying;
  94. }
  95. #pragma mark - Setter
  96. - (void)setDelegate:(id<DKAudioPlayerHelperDelegate>)delegate {
  97. if (_delegate != delegate) {
  98. _delegate = delegate;
  99. if (_delegate == nil) {
  100. [self stopAudio];
  101. }
  102. }
  103. }
  104. #pragma mark - Life Cycle
  105. + (id)shareInstance {
  106. static DKAudioPlayerHelper *instance = nil;
  107. static dispatch_once_t onceToken;
  108. dispatch_once(&onceToken, ^{
  109. instance = [[DKAudioPlayerHelper alloc] init];
  110. });
  111. return instance;
  112. }
  113. - (id)init {
  114. self = [super init];
  115. if (self) {
  116. [self changeProximityMonitorEnableState:YES];
  117. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
  118. }
  119. return self;
  120. }
  121. - (void)dealloc {
  122. [self changeProximityMonitorEnableState:NO];
  123. }
  124. #pragma mark - audio delegate
  125. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
  126. [self stopAudio];
  127. if ([self.delegate respondsToSelector:@selector(didAudioPlayerStopPlay:)]) {
  128. [self.delegate didAudioPlayerStopPlay:_player];
  129. }
  130. }
  131. #pragma mark - 近距离传感器
  132. - (void)changeProximityMonitorEnableState:(BOOL)enable {
  133. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
  134. if ([UIDevice currentDevice].proximityMonitoringEnabled == YES) {
  135. if (enable) {
  136. //添加近距离事件监听,添加前先设置为YES,如果设置完后还是NO的读话,说明当前设备没有近距离传感器
  137. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:UIDeviceProximityStateDidChangeNotification object:nil];
  138. } else {
  139. //删除近距离事件监听
  140. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceProximityStateDidChangeNotification object:nil];
  141. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
  142. }
  143. }
  144. }
  145. - (void)sensorStateChange:(NSNotificationCenter *)notification {
  146. //如果此时手机靠近面部放在耳朵旁,那么声音将通过听筒输出,并将屏幕变暗
  147. if ([[UIDevice currentDevice] proximityState] == YES) {
  148. //黑屏
  149. DLog(@"Device is close to user");
  150. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  151. } else {
  152. //没黑屏幕
  153. DLog(@"Device is not close to user");
  154. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  155. if (!_player || !_player.isPlaying) {
  156. //没有播放了,也没有在黑屏状态下,就可以把距离传感器关了
  157. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
  158. }
  159. }
  160. }
  161. @end