// // YOUPAILZMusicManager.m // YOUQU // // Created by CY on 2021/12/10. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAILZMusicManager.h" static YOUPAILZMusicManager *_manager; static dispatch_once_t _onceToken; @interface YOUPAILZMusicManager () @property (nonatomic, strong, readwrite) NSMutableArray *youpaipmusics; /// 音乐数据 @property (nonatomic, strong, readwrite) YOUPAILZMusicListItemModel *youpaipcurrentMusic; /// 当前正在播放的音乐 @property (nonatomic, assign, readwrite) BOOL youpaipisPlay; /// 是否播放 @property (nonatomic, assign, readwrite) BOOL youpaipisPause; /// 是否暂停 @property (nonatomic, assign, readwrite) BOOL youpaipisShowLyrie; /// 是否显示歌词 @property (nonatomic, assign) BOOL isFirst; // 是否是首次播放 @end @implementation YOUPAILZMusicManager + (instancetype)shareManager{ dispatch_once(&_onceToken, ^{ _manager = [[YOUPAILZMusicManager alloc] init]; [_manager youpaifrelaodMusicList]; }); return _manager; } /// 刷新音乐数据 - (void)youpaifrelaodMusicList{ self.youpaipisPlay = NO; self.youpaipisPause = YES; @weakify(self); [LCHttpHelper requestWithURLString:MySongs parameters:@{} needToken:YES type:(HttpRequestTypePost) success:^(id responseObject) { @strongify(self); NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) {//成功 NSDictionary *data = [dict objectForKey:@"data"]; self.youpaipmusics = [YOUPAILZMusicListItemModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"list"]]; } if (self.youpaipmusics.count == 0) { self.youpaipcurrentMusic = nil; return; }else{ self.youpaipcurrentMusic = self.youpaipmusics[0]; } if (self.changeCurrentMusicBlock != nil) { self.changeCurrentMusicBlock(self.youpaipcurrentMusic); } if (self.changePlayStateBlock != nil) { self.isFirst = YES; self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } failure:^(NSError *error) { @strongify(self); [self youpaifrelaodMusicList]; }]; } /// 下一首 - (void)youpaifnext{ if (self.youpaipmusics.count == 0) { return; } self.youpaipisPlay = YES; self.youpaipisPause = NO; self.youpaipcurrentMusic.youpaipisPlay = NO; if ([self.youpaipmusics containsObject:self.youpaipcurrentMusic]) { NSInteger index = [self.youpaipmusics indexOfObject:self.youpaipcurrentMusic]; if (index + 1 >= self.youpaipmusics.count) { self.youpaipcurrentMusic = self.youpaipmusics[0]; }else{ self.youpaipcurrentMusic = self.youpaipmusics[index + 1]; } }else{ self.youpaipcurrentMusic = self.youpaipmusics[0]; } if (self.changeCurrentMusicBlock != nil) { self.youpaipcurrentMusic.youpaipisPlay = YES; [[YOUPAILZChatRoomManager shareManager] youpaifloadMusicInfoWithModel:self.youpaipcurrentMusic]; self.changeCurrentMusicBlock(self.youpaipcurrentMusic); } if (self.changePlayStateBlock != nil) { self.isFirst = YES; self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } /// 上一首 - (void)youpaifprevious{ if (self.youpaipmusics.count == 0) { return; } self.youpaipisPlay = YES; self.youpaipisPause = NO; self.youpaipcurrentMusic.youpaipisPlay = NO; if ([self.youpaipmusics containsObject:self.youpaipcurrentMusic]) { NSInteger index = [self.youpaipmusics indexOfObject:self.youpaipcurrentMusic]; if (index - 1 < 0) { self.youpaipcurrentMusic = self.youpaipmusics[self.youpaipmusics.count - 1]; }else{ self.youpaipcurrentMusic = self.youpaipmusics[index - 1]; } }else{ self.youpaipcurrentMusic = self.youpaipmusics[0]; } if (self.changeCurrentMusicBlock != nil) { self.youpaipcurrentMusic.youpaipisPlay = YES; [[YOUPAILZChatRoomManager shareManager] youpaifloadMusicInfoWithModel:self.youpaipcurrentMusic]; self.changeCurrentMusicBlock(self.youpaipcurrentMusic); } if (self.changePlayStateBlock != nil) { self.isFirst = YES; self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } /// 播放 - (void)youpaifplay{ if (self.youpaipmusics.count == 0) { return; } self.youpaipisPlay = YES; self.youpaipisPause = NO; if (self.changePlayStateBlock != nil) { self.youpaipcurrentMusic.youpaipisPlay = YES; self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); self.isFirst = NO; [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } /// 暂停 - (void)youpaifpause{ if (self.youpaipmusics.count == 0) { return; } self.youpaipisPlay = NO; self.youpaipisPause = YES; if (self.changePlayStateBlock != nil) { self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } ///播放指定歌曲 - (void)youpaifplayMusic:(YOUPAILZMusicListItemModel *)music{ if (self.youpaipmusics.count == 0) { return; } self.youpaipisPlay = YES; self.youpaipisPause = NO; self.youpaipcurrentMusic.youpaipisPlay = NO; if ([self.youpaipmusics containsObject:music]) { NSInteger index = [self.youpaipmusics indexOfObject:music]; self.youpaipcurrentMusic = self.youpaipmusics[index]; }else{ self.youpaipcurrentMusic = self.youpaipmusics[0]; } if (self.changeCurrentMusicBlock != nil) { self.youpaipcurrentMusic.youpaipisPlay = YES; [[YOUPAILZChatRoomManager shareManager] youpaifloadMusicInfoWithModel:self.youpaipcurrentMusic]; self.changeCurrentMusicBlock(self.youpaipcurrentMusic); } if (self.changePlayStateBlock != nil) { self.isFirst = YES; self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } /// 添加歌曲 - (void)youpaifaddMusic:(YOUPAILZMusicListItemModel *)music{ [self.youpaipmusics addObject:music]; if (self.youpaipcurrentMusic == nil) { self.youpaipcurrentMusic = music; if (self.changeCurrentMusicBlock != nil) { self.changeCurrentMusicBlock(self.youpaipcurrentMusic); } } } /// 删除歌曲 - (void)youpaifremoveMusic:(YOUPAILZMusicListItemModel *)music{ NSInteger index = -1; for (NSInteger i = 0; i < self.youpaipmusics.count; i ++) { YOUPAILZMusicListItemModel *iMusic = self.youpaipmusics[i]; NSLog(@"%@ ==== %@",iMusic.youpaipsongCode, music.youpaipsongCode); if ([music.youpaipsongCode isEqual:iMusic.youpaipsongCode]) { // music = iMusic; index = i; break; } } if (index == -1) { return; } BOOL deleteCurrentMusic = [[self.youpaipmusics objectAtIndex:index].youpaipsongCode isEqual:self.youpaipcurrentMusic.youpaipsongCode]; [self.youpaipmusics removeObjectAtIndex:index]; if (deleteCurrentMusic) { self.youpaipisPlay = NO; self.youpaipisPause = YES; if (index >= self.youpaipmusics.count) { self.youpaipcurrentMusic = self.youpaipmusics.count == 0 ? nil : self.youpaipmusics[0]; }else{ self.youpaipcurrentMusic = self.youpaipmusics[index]; } if (self.changeCurrentMusicBlock != nil) { self.changeCurrentMusicBlock(self.youpaipcurrentMusic); } if (self.changePlayStateBlock != nil) { self.isFirst = YES; self.changePlayStateBlock(self.youpaipisPlay,self.isFirst); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } } /// 显示歌词 - (void)youpaifshowLyrie{ self.youpaipisShowLyrie = YES; if (self.changeShowLyrieStateBlock != nil) { self.changeShowLyrieStateBlock(self.youpaipisShowLyrie); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } /// 隐藏歌词 - (void)youpaifhideLyrie{ self.youpaipisShowLyrie = NO; if (self.changeShowLyrieStateBlock != nil) { self.changeShowLyrieStateBlock(self.youpaipisShowLyrie); [[NSNotificationCenter defaultCenter] postNotificationName:@"VQUMusicStateChangeNotification" object:nil userInfo:nil]; } } /// 停止播放 - (void)youpaifstopPlay{ self.isFirst = YES; } - (NSMutableArray *)youpaipmusics{ if (!_youpaipmusics) { _youpaipmusics = [NSMutableArray array]; } return _youpaipmusics; } @end