EMAudioPlayerUtil.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import "EMAudioPlayerUtil.h"
  13. #import <AVFoundation/AVFoundation.h>
  14. #import "EaseLocalDefine.h"
  15. static EMAudioPlayerUtil *audioPlayerUtil = nil;
  16. @interface EMAudioPlayerUtil () <AVAudioPlayerDelegate> {
  17. AVAudioPlayer *_player;
  18. void (^playFinish)(NSError *error);
  19. }
  20. @end
  21. @implementation EMAudioPlayerUtil
  22. #pragma mark - public
  23. + (BOOL)isPlaying{
  24. return [[EMAudioPlayerUtil sharedInstance] isPlaying];
  25. }
  26. + (NSString *)playingFilePath{
  27. return [[EMAudioPlayerUtil sharedInstance] playingFilePath];
  28. }
  29. + (void)asyncPlayingWithPath:(NSString *)aFilePath
  30. completion:(void(^)(NSError *error))completon{
  31. [[EMAudioPlayerUtil sharedInstance] asyncPlayingWithPath:aFilePath
  32. completion:completon];
  33. }
  34. + (void)stopCurrentPlaying{
  35. [[EMAudioPlayerUtil sharedInstance] stopCurrentPlaying];
  36. }
  37. #pragma mark - private
  38. + (EMAudioPlayerUtil *)sharedInstance{
  39. static dispatch_once_t onceToken;
  40. dispatch_once(&onceToken, ^{
  41. audioPlayerUtil = [[self alloc] init];
  42. });
  43. return audioPlayerUtil;
  44. }
  45. - (BOOL)isPlaying
  46. {
  47. return !!_player;
  48. }
  49. // Get the path of what is currently being played
  50. - (NSString *)playingFilePath
  51. {
  52. NSString *path = nil;
  53. if (_player && _player.isPlaying) {
  54. path = _player.url.path;
  55. }
  56. return path;
  57. }
  58. - (void)asyncPlayingWithPath:(NSString *)aFilePath
  59. completion:(void(^)(NSError *error))completon{
  60. playFinish = completon;
  61. NSError *error = nil;
  62. NSFileManager *fm = [NSFileManager defaultManager];
  63. if (![fm fileExistsAtPath:aFilePath]) {
  64. error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.notFound", @"File path not exist")
  65. code:-1
  66. userInfo:nil];
  67. if (playFinish) {
  68. playFinish(error);
  69. }
  70. playFinish = nil;
  71. return;
  72. }
  73. NSURL *wavUrl = [[NSURL alloc] initFileURLWithPath:aFilePath];
  74. _player = [[AVAudioPlayer alloc] initWithContentsOfURL:wavUrl error:&error];
  75. if (error || !_player) {
  76. _player = nil;
  77. error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.initPlayerFail", @"Failed to initialize AVAudioPlayer")
  78. code:-1
  79. userInfo:nil];
  80. if (playFinish) {
  81. playFinish(error);
  82. }
  83. playFinish = nil;
  84. return;
  85. }
  86. _player.delegate = self;
  87. [_player prepareToPlay];
  88. [_player play];
  89. }
  90. - (void)stopCurrentPlaying{
  91. if(_player){
  92. _player.delegate = nil;
  93. [_player stop];
  94. _player = nil;
  95. }
  96. if (playFinish) {
  97. playFinish = nil;
  98. }
  99. }
  100. - (void)dealloc{
  101. if (_player) {
  102. _player.delegate = nil;
  103. [_player stop];
  104. _player = nil;
  105. }
  106. playFinish = nil;
  107. }
  108. #pragma mark - AVAudioPlayerDelegate
  109. - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
  110. successfully:(BOOL)flag{
  111. if (playFinish) {
  112. playFinish(nil);
  113. }
  114. if (_player) {
  115. _player.delegate = nil;
  116. _player = nil;
  117. }
  118. playFinish = nil;
  119. }
  120. - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player
  121. error:(NSError *)error{
  122. if (playFinish) {
  123. NSError *error = [NSError errorWithDomain:NSEaseLocalizedString(@"error.palyFail", @"Play failure")
  124. code:-1
  125. userInfo:nil];
  126. playFinish(error);
  127. }
  128. if (_player) {
  129. _player.delegate = nil;
  130. _player = nil;
  131. }
  132. }
  133. @end