YOUPAILZMusicManager.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // YOUPAILZMusicManager.m
  3. // YOUQU
  4. //
  5. // Created by CY on 2021/12/10.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAILZMusicManager.h"
  9. static YOUPAILZMusicManager *_manager;
  10. static dispatch_once_t _onceToken;
  11. @interface YOUPAILZMusicManager ()
  12. @property (nonatomic, strong, readwrite) NSMutableArray <YOUPAILZMusicListItemModel *>*youpaipmusics; /// 音乐数据
  13. @property (nonatomic, strong, readwrite) YOUPAILZMusicListItemModel *youpaipcurrentMusic; /// 当前正在播放的音乐
  14. @property (nonatomic, assign, readwrite) BOOL youpaipisPlay; /// 是否播放
  15. @property (nonatomic, assign, readwrite) BOOL youpaipisPause; /// 是否暂停
  16. @property (nonatomic, assign, readwrite) BOOL youpaipisShowLyrie; /// 是否显示歌词
  17. @property (nonatomic, assign) BOOL isFirst; // 是否是首次播放
  18. @end
  19. @implementation YOUPAILZMusicManager
  20. + (instancetype)shareManager{
  21. dispatch_once(&_onceToken, ^{
  22. _manager = [[YOUPAILZMusicManager alloc] init];
  23. [_manager youpaifrelaodMusicList];
  24. });
  25. return _manager;
  26. }
  27. /// 刷新音乐数据
  28. - (void)youpaifrelaodMusicList{
  29. self.youpaipisPlay = NO;
  30. self.youpaipisPause = YES;
  31. @weakify(self);
  32. [LCHttpHelper requestWithURLString:MySongs parameters:@{} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) {
  33. @strongify(self);
  34. NSDictionary* dict = (NSDictionary*)responseObject;
  35. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  36. if (code == 0) {//成功
  37. NSDictionary *data = [dict objectForKey:@"data"];
  38. self.youpaipmusics = [YOUPAILZMusicListItemModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"list"]];
  39. }
  40. if (self.youpaipmusics.count == 0) {
  41. self.youpaipcurrentMusic = nil;
  42. return;
  43. }else{
  44. self.youpaipcurrentMusic = self.youpaipmusics[0];
  45. }
  46. if (self.changeCurrentMusicBlock != nil) {
  47. self.changeCurrentMusicBlock(self.youpaipcurrentMusic);
  48. }
  49. if (self.changePlayStateBlock != nil) {
  50. self.isFirst = YES;
  51. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  52. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  53. }
  54. } failure:^(NSError *error) {
  55. @strongify(self);
  56. [self youpaifrelaodMusicList];
  57. }];
  58. }
  59. /// 下一首
  60. - (void)youpaifnext{
  61. if (self.youpaipmusics.count == 0) {
  62. return;
  63. }
  64. self.youpaipisPlay = YES;
  65. self.youpaipisPause = NO;
  66. self.youpaipcurrentMusic.youpaipisPlay = NO;
  67. if ([self.youpaipmusics containsObject:self.youpaipcurrentMusic]) {
  68. NSInteger index = [self.youpaipmusics indexOfObject:self.youpaipcurrentMusic];
  69. if (index + 1 >= self.youpaipmusics.count) {
  70. self.youpaipcurrentMusic = self.youpaipmusics[0];
  71. }else{
  72. self.youpaipcurrentMusic = self.youpaipmusics[index + 1];
  73. }
  74. }else{
  75. self.youpaipcurrentMusic = self.youpaipmusics[0];
  76. }
  77. if (self.changeCurrentMusicBlock != nil) {
  78. self.youpaipcurrentMusic.youpaipisPlay = YES;
  79. [[YOUPAILZChatRoomManager shareManager] youpaifloadMusicInfoWithModel:self.youpaipcurrentMusic];
  80. self.changeCurrentMusicBlock(self.youpaipcurrentMusic);
  81. }
  82. if (self.changePlayStateBlock != nil) {
  83. self.isFirst = YES;
  84. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  85. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  86. }
  87. }
  88. /// 上一首
  89. - (void)youpaifprevious{
  90. if (self.youpaipmusics.count == 0) {
  91. return;
  92. }
  93. self.youpaipisPlay = YES;
  94. self.youpaipisPause = NO;
  95. self.youpaipcurrentMusic.youpaipisPlay = NO;
  96. if ([self.youpaipmusics containsObject:self.youpaipcurrentMusic]) {
  97. NSInteger index = [self.youpaipmusics indexOfObject:self.youpaipcurrentMusic];
  98. if (index - 1 < 0) {
  99. self.youpaipcurrentMusic = self.youpaipmusics[self.youpaipmusics.count - 1];
  100. }else{
  101. self.youpaipcurrentMusic = self.youpaipmusics[index - 1];
  102. }
  103. }else{
  104. self.youpaipcurrentMusic = self.youpaipmusics[0];
  105. }
  106. if (self.changeCurrentMusicBlock != nil) {
  107. self.youpaipcurrentMusic.youpaipisPlay = YES;
  108. [[YOUPAILZChatRoomManager shareManager] youpaifloadMusicInfoWithModel:self.youpaipcurrentMusic];
  109. self.changeCurrentMusicBlock(self.youpaipcurrentMusic);
  110. }
  111. if (self.changePlayStateBlock != nil) {
  112. self.isFirst = YES;
  113. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  114. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  115. }
  116. }
  117. /// 播放
  118. - (void)youpaifplay{
  119. if (self.youpaipmusics.count == 0) {
  120. return;
  121. }
  122. self.youpaipisPlay = YES;
  123. self.youpaipisPause = NO;
  124. if (self.changePlayStateBlock != nil) {
  125. self.youpaipcurrentMusic.youpaipisPlay = YES;
  126. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  127. self.isFirst = NO;
  128. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  129. }
  130. }
  131. /// 暂停
  132. - (void)youpaifpause{
  133. if (self.youpaipmusics.count == 0) {
  134. return;
  135. }
  136. self.youpaipisPlay = NO;
  137. self.youpaipisPause = YES;
  138. if (self.changePlayStateBlock != nil) {
  139. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  140. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  141. }
  142. }
  143. ///播放指定歌曲
  144. - (void)youpaifplayMusic:(YOUPAILZMusicListItemModel *)music{
  145. if (self.youpaipmusics.count == 0) {
  146. return;
  147. }
  148. self.youpaipisPlay = YES;
  149. self.youpaipisPause = NO;
  150. self.youpaipcurrentMusic.youpaipisPlay = NO;
  151. if ([self.youpaipmusics containsObject:music]) {
  152. NSInteger index = [self.youpaipmusics indexOfObject:music];
  153. self.youpaipcurrentMusic = self.youpaipmusics[index];
  154. }else{
  155. self.youpaipcurrentMusic = self.youpaipmusics[0];
  156. }
  157. if (self.changeCurrentMusicBlock != nil) {
  158. self.youpaipcurrentMusic.youpaipisPlay = YES;
  159. [[YOUPAILZChatRoomManager shareManager] youpaifloadMusicInfoWithModel:self.youpaipcurrentMusic];
  160. self.changeCurrentMusicBlock(self.youpaipcurrentMusic);
  161. }
  162. if (self.changePlayStateBlock != nil) {
  163. self.isFirst = YES;
  164. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  165. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  166. }
  167. }
  168. /// 添加歌曲
  169. - (void)youpaifaddMusic:(YOUPAILZMusicListItemModel *)music{
  170. [self.youpaipmusics addObject:music];
  171. if (self.youpaipcurrentMusic == nil) {
  172. self.youpaipcurrentMusic = music;
  173. if (self.changeCurrentMusicBlock != nil) {
  174. self.changeCurrentMusicBlock(self.youpaipcurrentMusic);
  175. }
  176. }
  177. }
  178. /// 删除歌曲
  179. - (void)youpaifremoveMusic:(YOUPAILZMusicListItemModel *)music{
  180. NSInteger index = -1;
  181. for (NSInteger i = 0; i < self.youpaipmusics.count; i ++) {
  182. YOUPAILZMusicListItemModel *iMusic = self.youpaipmusics[i];
  183. NSLog(@"%@ ==== %@",iMusic.youpaipsongCode, music.youpaipsongCode);
  184. if ([music.youpaipsongCode isEqual:iMusic.youpaipsongCode]) {
  185. // music = iMusic;
  186. index = i;
  187. break;
  188. }
  189. }
  190. if (index == -1) {
  191. return;
  192. }
  193. BOOL deleteCurrentMusic = [[self.youpaipmusics objectAtIndex:index].youpaipsongCode isEqual:self.youpaipcurrentMusic.youpaipsongCode];
  194. [self.youpaipmusics removeObjectAtIndex:index];
  195. if (deleteCurrentMusic) {
  196. self.youpaipisPlay = NO;
  197. self.youpaipisPause = YES;
  198. if (index >= self.youpaipmusics.count) {
  199. self.youpaipcurrentMusic = self.youpaipmusics.count == 0 ? nil : self.youpaipmusics[0];
  200. }else{
  201. self.youpaipcurrentMusic = self.youpaipmusics[index];
  202. }
  203. if (self.changeCurrentMusicBlock != nil) {
  204. self.changeCurrentMusicBlock(self.youpaipcurrentMusic);
  205. }
  206. if (self.changePlayStateBlock != nil) {
  207. self.isFirst = YES;
  208. self.changePlayStateBlock(self.youpaipisPlay,self.isFirst);
  209. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  210. }
  211. }
  212. }
  213. /// 显示歌词
  214. - (void)youpaifshowLyrie{
  215. self.youpaipisShowLyrie = YES;
  216. if (self.changeShowLyrieStateBlock != nil) {
  217. self.changeShowLyrieStateBlock(self.youpaipisShowLyrie);
  218. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  219. }
  220. }
  221. /// 隐藏歌词
  222. - (void)youpaifhideLyrie{
  223. self.youpaipisShowLyrie = NO;
  224. if (self.changeShowLyrieStateBlock != nil) {
  225. self.changeShowLyrieStateBlock(self.youpaipisShowLyrie);
  226. [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil];
  227. }
  228. }
  229. /// 停止播放
  230. - (void)youpaifstopPlay{
  231. self.isFirst = YES;
  232. }
  233. - (NSMutableArray<YOUPAILZMusicListItemModel *> *)youpaipmusics{
  234. if (!_youpaipmusics) {
  235. _youpaipmusics = [NSMutableArray array];
  236. }
  237. return _youpaipmusics;
  238. }
  239. @end