123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- //
- // 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 <YOUPAILZMusicListItemModel *>*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<YOUPAILZMusicListItemModel *> *)youpaipmusics{
- if (!_youpaipmusics) {
- _youpaipmusics = [NSMutableArray array];
- }
- return _youpaipmusics;
- }
- @end
|