JCHATAudioPlayerHelper.m 7.2 KB

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