FUMusicPlayer.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // FUMusicPlayer.m
  3. // FULive
  4. //
  5. // Created by 刘洋 on 2017/10/13.
  6. // Copyright © 2017年 liuyang. All rights reserved.
  7. //
  8. #import "FUMusicPlayer.h"
  9. #import <AVFoundation/AVFoundation.h>
  10. @implementation FUMusicPlayer
  11. {
  12. AVAudioPlayer *audioPlayer;
  13. NSString *musicName;
  14. }
  15. + (FUMusicPlayer *)sharePlayer{
  16. static FUMusicPlayer *_sharePlayer;
  17. static dispatch_once_t onceToken;
  18. dispatch_once(&onceToken, ^{
  19. _sharePlayer = [[FUMusicPlayer alloc] init];
  20. _sharePlayer.enable = YES;
  21. });
  22. return _sharePlayer;
  23. }
  24. -(instancetype)init{
  25. if (self = [super init]) {
  26. [self setupAudioSession];
  27. }
  28. return self;
  29. }
  30. - (void)setupAudioSession {
  31. static BOOL audioSessionSetup = NO;
  32. if (audioSessionSetup) {
  33. return;
  34. }
  35. [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error: nil];
  36. UInt32 doSetProperty = 1;
  37. AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
  38. [[AVAudioSession sharedInstance] setActive: YES error: nil];
  39. audioSessionSetup = YES;
  40. }
  41. - (void)setEnable:(BOOL)enable{
  42. if (_enable == enable) {
  43. return;
  44. }
  45. _enable = enable;
  46. dispatch_async(dispatch_get_main_queue(), ^{
  47. if (enable) {
  48. [self rePlay];
  49. }else [self pause];
  50. });
  51. }
  52. - (void)playMusic:(NSString *)music{
  53. dispatch_async(dispatch_get_main_queue(), ^{
  54. if ([audioPlayer isPlaying]) {
  55. [audioPlayer stop];
  56. audioPlayer = nil;
  57. }
  58. if (music) {
  59. NSString *path = [[NSBundle mainBundle] pathForResource:music ofType:nil];
  60. if (path) {
  61. NSURL *musicUrl = [NSURL fileURLWithPath:path];
  62. if (musicUrl) {
  63. musicName = music;
  64. if (self.enable) {
  65. audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicUrl error:nil];
  66. audioPlayer.numberOfLoops = 100000;
  67. [audioPlayer play];
  68. }
  69. }
  70. }
  71. }
  72. });
  73. }
  74. - (void)resume{
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. if (![audioPlayer isPlaying]) {
  77. [audioPlayer play];
  78. }
  79. });
  80. }
  81. - (void)rePlay{
  82. dispatch_async(dispatch_get_main_queue(), ^{
  83. [self playMusic:musicName];
  84. });
  85. }
  86. - (void)pause{
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. if ([audioPlayer isPlaying]) {
  89. [audioPlayer pause];
  90. }
  91. });
  92. }
  93. - (void)stop{
  94. dispatch_async(dispatch_get_main_queue(), ^{
  95. if ([audioPlayer isPlaying]) {
  96. [audioPlayer stop];
  97. }
  98. audioPlayer = nil;
  99. });
  100. }
  101. - (float)playProgress {
  102. if (audioPlayer.duration > 0) {
  103. return audioPlayer.currentTime / (audioPlayer.duration);
  104. }else return 0.0;
  105. }
  106. - (NSTimeInterval)currentTime {
  107. return audioPlayer.currentTime;
  108. }
  109. @end