CWAudioPlayer.m 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // CWAudioPlayer.m
  3. // CWAudioTool
  4. //
  5. // Created by chavez on 2017/9/26.
  6. // Copyright © 2017年 chavez. All rights reserved.
  7. //
  8. #import "CWAudioPlayer.h"
  9. @interface CWAudioPlayer()
  10. /** 音频播放器 */
  11. @property (nonatomic ,strong) AVAudioPlayer *player;
  12. @end
  13. @implementation CWAudioPlayer
  14. singtonImplement(CWAudioPlayer);
  15. - (AVAudioPlayer *)playAudioWith:(NSString *)audioPath {
  16. [self stopCurrentAudio]; // 播放之前 先结束当前播放
  17. // 设置为扬声器播放
  18. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  19. NSURL *url = [NSURL URLWithString:audioPath];
  20. if (url == nil) {
  21. url = [[NSBundle mainBundle] URLForResource:audioPath.lastPathComponent withExtension:nil];
  22. }
  23. self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
  24. NSLog(ASLocalizedString(@"准备播放...%@"),url);
  25. [self.player prepareToPlay];
  26. // NSLog(ASLocalizedString(@"播放..."));
  27. [self.player play];
  28. return self.player;
  29. }
  30. - (void)resumeCurrentAudio {
  31. [self.player play];
  32. }
  33. - (void)pauseCurrentAudio {
  34. [self.player pause];
  35. }
  36. - (void)stopCurrentAudio {
  37. [self.player stop];
  38. }
  39. - (float)progress {
  40. return self.player.currentTime / self.player.duration;
  41. }
  42. @end