//
//  KKPlayerViewController.m
//  Open
//
//  Created by 雷俊博 on 2020/6/11.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "KKPlayerViewController.h"
#import "UIImageView+WebCache.h"
#import "UIView+frameAdjust.h"
#import <AVFoundation/AVFoundation.h>
#import "XMSDKPlayer.h"
#import "KKPlayerListView.h"
#import "ICMusicPlayManager.h"
#import <MediaPlayer/MediaPlayer.h>

@interface KKPlayerViewController (){
    XMSDKPlayer *_sdkPlayer;
    XMTrack* markTrackModel;
    NSInteger _index;
    NSInteger currentPage;
}

@property (nonatomic, strong)UIImageView* backImg;

@property (nonatomic, strong)UIImageView* iconImg;

@property (nonatomic, strong)UILabel*lb_title;

@property(nonatomic, strong) UIView *middlePlayView;                //中间视图展示
@property(nonatomic, strong) UIProgressView *progressView;          //缓冲进度条
@property(nonatomic, strong) UISlider *sliderView;                  //播放进度条
@property(nonatomic, strong) UILabel *beginTime;                    //起始时间
@property(nonatomic, strong) UILabel *endTime;                      //结束时间

@property(nonatomic, strong) UIButton *formerBtn;                   //上一首
@property(nonatomic, strong) UIButton *playBtn;                     //播放、暂停
@property(nonatomic, strong) UIButton *nextBtn;                     //下一首

@property (nonatomic, strong)UIButton* listBtn;                     //播放列表

@property (nonatomic, strong)UIButton* closeBtn;                    //关闭按钮

@property (nonatomic, strong)UILabel* lb_message;                   //内容来源

@property (nonatomic, strong)KKPlayerListView* listView;





@end

@implementation KKPlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    _sdkPlayer = [XMSDKPlayer sharedPlayer];
    [_sdkPlayer setPlayMode:XMSDKPlayModeTrack];
    [_sdkPlayer setTrackPlayMode:XMTrackPlayerModeList];
    [_sdkPlayer setAutoNexTrack:YES];
    
    
   if (self.dataArray == nil) {
     self.dataArray = [_sdkPlayer playList];
     self.currentPage = [ICMusicPlayManager sharedManager].currentPage;
    }
    
    [self creatUI];
    
    [self navConfig];
    
    [self initWithListView];
   
    markTrackModel = self.model;
}

- (NSInteger)getIndex
{
    XMTrack*currentTrack = [_sdkPlayer currentTrack];
    NSArray*array = [_sdkPlayer playList];
    NSInteger index = 0;
    for (int i = 0; i < array.count; i++) {
        XMTrack* track = array[i];
        if (track.trackId == currentTrack.trackId) {
            index = i;
            break;
        }
    }
    
    return index;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
    WeakSelf
       ICMusicPlayManager*manager = [ICMusicPlayManager sharedManager];
    manager.KKPlayerWillPlaying = ^{
        
    };
    manager.KKPlayerDidError = ^(NSString * _Nonnull type, NSDictionary * _Nonnull data) {
        [SVProgressHUD dismiss];
    };
       manager.KKPlayerDidStart = ^{
           [SVProgressHUD dismiss];
              XMTrack*track = [XMSDKPlayer sharedPlayer].currentTrack;
              weakSelf.lb_title.text = track.trackTitle;
              
              weakSelf.sliderView.value = 0.0;
              weakSelf.beginTime.text = @"00:00";
              weakSelf.endTime.text = [self timeFormatted:track.duration];
              
              //CGFloat duration = (CGFloat)track.duration;
              
              [weakSelf startRotatingWithView:weakSelf.iconImg];
           
           [self setupLockScreenMediaInfo:track];
           [self SaveTrackDataRequest:track];
              
       };
       manager.KKPlayerDidPaused = ^{
          
       };
       manager.KKPlayerDidPlaying = ^{
          
       };
       manager.KKPlayerPlayTimeChanged = ^(CGFloat percent, NSUInteger currentSecond) {
              weakSelf.sliderView.value =percent;
              weakSelf.beginTime.text = [weakSelf timeFormatted:currentSecond];
       };
    manager.KKPlayerDidPlaylistEnd = ^{
        [self closeBtnAction];
    };
    
    if (_sdkPlayer.isPlaying) {
        [self startRotatingWithView:weakSelf.iconImg];
    }
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    
//    [[XMSDKPlayer sharedPlayer] stopTrackPlay];
    
    [super viewDidDisappear:animated];
    
   // [XMSDKPlayer sharedPlayer].trackPlayDelegate = nil;
   // [XMSDKPlayer sharedPlayer].livePlayDelegate = nil;
    
}

- (void)creatUI
{
    self.backImg = [[UIImageView alloc]init];
    self.backImg.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
    self.backImg.contentMode = UIViewContentModeScaleAspectFill;
    self.backImg.userInteractionEnabled = YES;
    self.backImg.layer.masksToBounds = YES;
    [self.view addSubview:self.backImg];
    
   
    
    UIToolbar*bar = [[UIToolbar alloc]initWithFrame:self.backImg.bounds];
    bar.barStyle = UIBarStyleBlack;
    bar.alpha = 1.0;
    [self.backImg addSubview:bar];
    
    //title
    UILabel*lb_title = [[UILabel alloc]init];
    lb_title.textColor = [UIColor whiteColor];
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.font = [UIFont boldSystemFontOfSize:19 * WIDTHRADIU];
    self.lb_title = lb_title;
    [self.backImg addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(cl_kNavigationBarHeight + 20* WIDTHRADIU);
        make.left.offset(20 * WIDTHRADIU);
        make.right.offset(-20 * WIDTHRADIU);
    }];
    
    UIToolbar*bar2 = [[UIToolbar alloc]init];
    bar2.barStyle = UIBarStyleBlack;
    bar2.alpha = 1.0;
    [self.backImg addSubview:bar2];
    
    //头像
    UIImageView*iconImg = [[UIImageView alloc]init];
    iconImg.backgroundColor = [UIColor whiteColor];
    [self.backImg addSubview:iconImg];
    iconImg.layer.borderWidth = 4 * WIDTHRADIU;
    iconImg.layer.borderColor = [UIColor whiteColor].CGColor;
    iconImg.layer.cornerRadius = 0.55 * ScreenWidth/2;
    iconImg.layer.masksToBounds = YES;
    
   
    self.iconImg = iconImg;
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(75 * WIDTHRADIU);
        make.centerX.equalTo(self.backImg);
        make.width.height.offset(0.55 * ScreenWidth);
    }];
    
    
    _middlePlayView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight - 160 * WIDTHRADIU, ScreenWidth, 160 * WIDTHRADIU)];
    _middlePlayView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_middlePlayView];
    
    _sliderView = [[UISlider alloc]initWithFrame:CGRectMake(12,12, ScreenWidth - 24, 30)];
    _sliderView.layer.masksToBounds = YES;
    _sliderView.layer.cornerRadius = 1.5;
    _sliderView.minimumValue = 0;
    _sliderView.maximumValue = 1.0;
    //_sliderView.value = self.currentProgress;
    _sliderView.continuous = YES;
    _sliderView.minimumTrackTintColor = [UIColor whiteColor];
    _sliderView.maximumTrackTintColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    [_sliderView addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

    //[_sliderView addTarget:self action:@selector(sliderTouchUpInSide:) forControlEvents:UIControlEventTouchUpInside];
    [_sliderView setThumbImage:[UIImage imageNamed:@"fullView_progressunselect"] forState:UIControlStateNormal];
    [_sliderView setThumbImage:[UIImage imageNamed:@"fullView_progressSelected"] forState:UIControlStateHighlighted];
    [_middlePlayView addSubview:_sliderView];
    
//    _progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(12,21, ScreenWidth - 24, 3)];
//    _progressView.layer.masksToBounds = YES;
//    _progressView.layer.cornerRadius = 1.5;
//    _progressView.trackTintColor= [UIColor clearColor];
//    _progressView.progressTintColor= [UIColor colorWithHex:0xe8e8e8];      //缓冲进度条上进度的颜色
//    _progressView.progress = 0.5;
//    [_middlePlayView addSubview:_progressView];
    
    _beginTime = [UILabel new];
    _beginTime.minX = _sliderView.minX;
    _beginTime.minY = _sliderView.maxY + 2;
    _beginTime.width = 80;
    _beginTime.height = 14;
    _beginTime.text = @"00:00";
    _beginTime.font = [UIFont systemFontOfSize:10];
    _beginTime.textColor = [UIColor whiteColor];
    [_middlePlayView addSubview:_beginTime];
    
    _endTime = [UILabel new];
    _endTime.minX = _sliderView.maxX - 80;
    _endTime.minY = _sliderView.maxY + 2;
    _endTime.width = 80;
    _endTime.height = 14;
    _endTime.textAlignment = NSTextAlignmentRight;
    _endTime.text = @"00:00";
    _endTime.font = [UIFont systemFontOfSize:10];
    _endTime.textColor = [UIColor whiteColor];
    [_middlePlayView addSubview:_endTime];
    
    
    _playBtn = [UIButton new];
    _playBtn.width = 62;
    _playBtn.height = 70;
    _playBtn.minY = 61;
    _playBtn.minX = ScreenWidth / 2 - 39;
    [_playBtn setImage:[UIImage imageNamed:@"play_pause"] forState:UIControlStateSelected];
    [_playBtn setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateNormal];
    [_playBtn addTarget:self action:@selector(playBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    //_playBtn.selected = !_isPlaying;
    [_middlePlayView addSubview:_playBtn];
    
    _formerBtn = [UIButton new];
    _formerBtn.width = 34;
    _formerBtn.height = 40;
    _formerBtn.minX = (ScreenWidth / 2 - 39 - 48 ) / 2 - 23 + 48 + 20;
    _formerBtn.centerY = _playBtn.centerY;
    [_formerBtn setImage:[UIImage imageNamed:@"playlast"] forState:UIControlStateNormal];
    [_formerBtn addTarget:self action:@selector(formerBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_middlePlayView addSubview:_formerBtn];
       
    _nextBtn = [UIButton new];
    _nextBtn.width = 34;
    _nextBtn.height = 40;
    _nextBtn.minX = ScreenWidth / 2 + 39 + (ScreenWidth / 2 - 39 - 48 ) / 2 - 23 - 20;
    _nextBtn.centerY = _playBtn.centerY;
    [_nextBtn setImage:[UIImage imageNamed:@"playnext"] forState:UIControlStateNormal];
    [_nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_middlePlayView addSubview:_nextBtn];
    
    //播放列表
    _listBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _listBtn.width = 60 ;
    _listBtn.height = 50;
    _listBtn.minX = 10;
    _listBtn.centerY = _playBtn.centerY;
    _listBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_listBtn setImage:[UIImage imageNamed:@"playlist"] forState:UIControlStateNormal];
    [_listBtn setTitle:@"播放列表" forState:UIControlStateNormal];
    [_listBtn addTarget:self action:@selector(playListBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_listBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:10];
    [_middlePlayView addSubview:_listBtn];
    
    //播放列表
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeBtn.width = 70 ;
    _closeBtn.height = 50;
    _closeBtn.minX = ScreenWidth - 80;
    _closeBtn.centerY = _playBtn.centerY;
    _closeBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_closeBtn setImage:[UIImage imageNamed:@"playClose"] forState:UIControlStateNormal];
    [_closeBtn setTitle:@"关闭播放器" forState:UIControlStateNormal];
    [_closeBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:10];
    [_closeBtn addTarget:self action:@selector(closeBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [_middlePlayView addSubview:_closeBtn];
    
    
    _lb_message = [UILabel new];
    _lb_message.text = @"内容来源:喜马拉雅APP";
    _lb_message.textColor = [UIColor whiteColor];
    _lb_message.textAlignment = NSTextAlignmentCenter;
    _lb_message.font = [UIFont boldSystemFontOfSize:10 * WIDTHRADIU];
    _lb_message.width = 150 ;
    _lb_message.height = 20;
    _lb_message.minY = _playBtn.maxY ;
    _lb_message.centerX = _middlePlayView.centerX;
    [_middlePlayView addSubview:_lb_message];
    
    
   
    
    
    if (self.model) {
        XMTrack*kModel = [_sdkPlayer currentTrack];
        if (self.model.trackId != kModel.trackId ) {
            [self play:self.model];
        }else{
            if (_sdkPlayer.isPlaying == NO) {
                [self play:self.model];
            }
        }
         
    }else{
        if (self.model == nil) {
               self.model = [_sdkPlayer currentTrack];
           }
           
           if (self.dataArray == nil) {
               self.dataArray = [_sdkPlayer playList];
               self.currentPage = [ICMusicPlayManager sharedManager].currentPage;
           }
        
        if (_sdkPlayer.isPlaying) {
            [self resume];
        }else{
           [self play:self.model];
        }
    }
    
   
    
    [self.backImg sd_setImageWithURL:[NSURL URLWithString:self.model.coverUrlLarge]];
    [iconImg sd_setImageWithURL:[NSURL URLWithString:self.model.coverUrlLarge]];
     lb_title.text = self.model.trackTitle;
    _endTime.text = [self timeFormatted:self.model.duration];
//    self.hasLastMusic = [XMSDKPlayer hasPrevTrack];
//    self.hasNextMusic = [XMSDKPlayer hasNextTrack];
    
     
}

- (void)initWithListView
{
    ICMusicPlayManager*manager = [ICMusicPlayManager sharedManager];
    _listView = [[KKPlayerListView alloc]initWithData:self.dataArray index:[self getIndex] model:manager.albumModel currentPage:self.currentPage];
    _listView.hidden = YES;
    WeakSelf
    _listView.KKPlayerListViewDidSelectedBlock = ^(XMTrack * _Nonnull track, NSArray * _Nonnull dataArray, NSInteger index) {
        __strong typeof(self) strongSelf = weakSelf;
        strongSelf.dataArray = dataArray;
        [strongSelf->_sdkPlayer replacePlayList:dataArray];
        if (track.playing) {
            return;
        }
        if (track.pause) {
            [strongSelf resume];
        }
        
        if (!track.playing && !track.pause) {
            [strongSelf play:track];
            
            //[strongSelf SaveTrackDataRequest:track];
            
        }
    };
    _listView.KKPlayerListViewHiddenBlock = ^{
        __strong typeof(self) strongSelf = weakSelf;
        strongSelf.listView.hidden = YES;
    };
    [self.view addSubview:_listView];
}

- (void)navConfig
{
    [self.navigationController setNavigationBarHidden:YES];
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake( 10 * WIDTHRADIU, cl_kStatusBarHeight + 9.5, 20, 25);
    backBtn.titleLabel.textAlignment =NSTextAlignmentLeft;
    //    [button_2 setTitle:@"asdasd按时打发第三方" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    //    [button_2 setBackgroundColor:[UIColor redColor]];
    //    [button_2 setTitleColor:[UIColor colorWithRed:12.0 green:12.0 blue:12.0 alpha:1] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.backImg addSubview:backBtn];
    
}


#pragma mark - event response
- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)playBtnAction:(UIButton *)button {
    //播放、暂停
    button.selected = !button.selected;
    if (button.selected) {
        [_playBtn setImage:[UIImage imageNamed:@"play_pause"] forState:UIControlStateSelected];
        [self pause];
    }else{
        [_playBtn setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateNormal];
        [self resume];
        
    }
}

- (void)formerBtnAction {
    //上一曲,先处理定时
   if(self.dataArray.count){
       [[XMSDKPlayer sharedPlayer] playPrevTrack];
       [self.playBtn setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateNormal];
       self.playBtn.selected = NO;
   }
}
- (void)nextBtnAction {
    //下一曲,先处理定时
   if(self.dataArray.count){
       [[XMSDKPlayer sharedPlayer] playNextTrack];
       [self.playBtn setImage:[UIImage imageNamed:@"play_on"] forState:UIControlStateNormal];
       self.playBtn.selected = NO;
   }
}

- (void)closeBtnAction {
    [self stop];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)sliderValueChanged:(id)sender
{
    if(self.dataArray.count){
                NSInteger second = [XMSDKPlayer sharedPlayer].currentTrack.duration*_sliderView.value;
    //            NSLog(@"seek to %ld   total : %ld",(long)second,(long)[XMSDKPlayer sharedPlayer].currentTrack.duration);
                [[XMSDKPlayer sharedPlayer] seekToTime:second];
                
                
    //            if (proView.value == 1.0) {
    //                [[XMSDKPlayer sharedPlayer] pauseTrackPlay];
    //            }
    }
}


#pragma mark - 中间播放控制视图按钮
- (void)playListBtnAction {
   
    [self listViewShow];
    
}

#pragma mark - 动画
- (void)startRotatingWithView:(UIView *)view
{
     CABasicAnimation* rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
     rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
     rotateAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2];
    rotateAnimation.duration = 20;
    rotateAnimation.repeatCount = MAXFLOAT;
    [view.layer addAnimation:rotateAnimation forKey:nil];
}

//暂停动画
-(void)animationPauseWithView:(UIView *)view{
    //获取当前layer的动画媒体时间
    CFTimeInterval interval = [view.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //设置时间偏移量,保证停留在当前位置
    view.layer.timeOffset = interval;
    //暂定动画
    view.layer.speed = 0;
}
//恢复动画
-(void)animationResumeWithView:(UIView *)view{
    //获取暂停的时间
    CFTimeInterval beginTime = CACurrentMediaTime() - view.layer.timeOffset;
    //设置偏移量
    view.layer.timeOffset = 0;
    //设置开始时间
    view.layer.beginTime = beginTime;
    //开始动画
    view.layer.speed = 1;
}

#pragma mark - 显示
- (void)listViewShow
{
    _listView.hidden = NO;
    _listView.currentIndex = [self getIndex];
    
    [_listView.tableView reloadData];
    
}

#pragma mark - 隐藏
- (void)listViewHidden
{
    _listView.hidden = YES;
}

#pragma mark - 播放音乐
- (void)play:(XMTrack *)track
{
    
    [SVProgressHUD show];
    if(self.dataArray.count){
        
        [_sdkPlayer setPlayMode:XMSDKPlayModeTrack];
        [_sdkPlayer setTrackPlayMode:XMTrackPlayerModeList];
        [_sdkPlayer playWithTrack:track playlist:self.dataArray];
        [_sdkPlayer setAutoNexTrack:YES];
    }
    
    if (self.currentPage > 0) {
           ICMusicPlayManager* manager = [ICMusicPlayManager sharedManager];
           manager.currentPage = currentPage;
       }
}

- (void)pause
{
    
    [self animationPauseWithView:self.iconImg];
    if(self.dataArray.count){
        [_sdkPlayer pauseTrackPlay];
    }
}
  
- (void)resume
{
    [self animationResumeWithView:self.iconImg];
    if (self.dataArray.count) {
        [_sdkPlayer resumeTrackPlay];
    }
    if (self.currentPage > 0) {
           ICMusicPlayManager* manager = [ICMusicPlayManager sharedManager];
           manager.currentPage = currentPage;
       }
}

-(void)stop
{
    if(self.dataArray.count){
        [_sdkPlayer stopTrackPlay];
    }
}

//存储播放记录
- (NSDictionary *)SaveTrackDataRequestPara:(XMTrack *)track
{
    
    NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSString*phone = nil;
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstlog"]) {
        phone = adId;
    }else{
        phone = [EnercryTool encryptUseDES:[getPeopleID PeopleIphoneNmber] key:DESKEY];
    }
    
    ICMusicPlayManager*manager = [ICMusicPlayManager sharedManager];
    NSDictionary*para = @{
        @"albumid":@(track.subordinatedAlbum.albumId),
        @"trackid":@(track.trackId),
        @"albumtitle":[NSString validBlankStr:track.subordinatedAlbum.albumTitle],
        @"pageindex":[NSString stringWithFormat:@"%ld",track.orderNum + 1],
        @"allpageindex":@(manager.albumModel.includeTrackCount),
        @"imageurl":[NSString validBlankStr:track.subordinatedAlbum.coverUrlLarge],
        @"phone":phone,
        @"listentitle":track.trackTitle,
        @"listenindex":@(track.orderNum)
    };
    return para;
}

- (void)SaveTrackDataRequest:(XMTrack *)track
{
    NSString* url = [NSString stringWithFormat:@"%@%@",[NetworkConfig ipXMLYHistoryConfig],XMLYSaveHistoryRequest];
    
    [NetworkRequest Post10:url Params:[self SaveTrackDataRequestPara:track] Success:^(id _Nonnull objc) {
        NSLog(@"objc:%@",objc);
    } Failure:^(NSError* error) {
        NSLog(@"error:%@",error);
    }];
}
    
#pragma mark - 锁频控制
//更新通知中心控制台媒体信息
- (void)setupLockScreenMediaInfo:(XMTrack *)track {
    
    MPNowPlayingInfoCenter *playingCenter = [MPNowPlayingInfoCenter defaultCenter];
    
    NSMutableDictionary *playingInfo = [NSMutableDictionary new];
    //标题
    playingInfo[MPMediaItemPropertyTitle]      = track.trackTitle;
    //封面图片
    UIImage *coverImage = nil;
    if ([NSString blankString:track.coverUrlLarge]) {
        coverImage = [UIImage imageNamed:@"trackPlaceImg"];
    }else {
        UIImageView *coverImageView = [[UIImageView alloc] init];
        [coverImageView sd_setImageWithURL:[NSURL URLWithString:track.coverUrlSmall]];
        coverImage = coverImageView.image;
        if (coverImage == nil) {
            coverImage = [UIImage imageNamed:@"trackPlaceImg"];
        }
    }
    MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:coverImage];
    playingInfo[MPMediaItemPropertyArtwork] = artwork;
    
    // 当前播放的时间
    NSLog(@"playingInfo . currentTime = %f",track.duration / 1000.0);
    playingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithFloat:100 / 1000];
    // 进度的速度
    playingInfo[MPNowPlayingInfoPropertyPlaybackRate] = [NSNumber numberWithFloat:1.0];
    // 总时间
    playingInfo[MPMediaItemPropertyPlaybackDuration] = [NSNumber numberWithFloat:track.duration / 1000];
//    if (@available(iOS 10.0, *)) {
//        playingInfo[MPNowPlayingInfoPropertyPlaybackProgress] = [NSNumber numberWithFloat:self.audioControlView.progress];
//    } else {
//        // Fallback on earlier versions
//    }
    [playingCenter setNowPlayingInfo:playingInfo];
}

    
#pragma mark - data
- (NSMutableArray *)showReceivedData:(id)result className:(NSString*)className valuePath:(NSString *)path titleNeedShow:(NSString*)title
{
    NSMutableArray *models = [NSMutableArray array];
    Class dataClass = NSClassFromString(className);
    if([result isKindOfClass:[NSArray class]]){
        for (NSDictionary *dic in result) {
            id model = [[dataClass alloc] initWithDictionary:dic];
            [models addObject:model];
        }
    }
    else if([result isKindOfClass:[NSDictionary class]]){
        if(path.length == 0)
        {
            id model = [[dataClass alloc] initWithDictionary:result];
            [models addObject:model];
        }
        else
        {
            NSArray *paths = [path componentsSeparatedByString:@"."];
            
//            NSLog(@"paths have %lu objects inside", (unsigned long)[paths count]);
//            NSLog(@"paths last object is %@", paths.lastObject);
            
            NSDictionary *dic = result;
            for (int i=0;i<paths.count-1;i++)
            {
                NSString *subPath = paths[i];
                dic = dic[subPath];
            }
            for (NSDictionary *dict in dic[paths.lastObject])
            {
                id model = [[dataClass alloc] initWithDictionary:dict];
                [models addObject:model];
                
            }
        }
    }
    
    return models;
}

#pragma mark - 秒数转化成时间
- (NSString *)timeFormatted:(NSInteger)totalSeconds

{

int seconds = totalSeconds % 60;

int minutes = (totalSeconds / 60) % 60;


return [NSString stringWithFormat:@"%02d:%02d", minutes, seconds];

}




#pragma mark -- settters
- (void)setHasNextMusic:(BOOL)hasNextMusic {
    _hasNextMusic = hasNextMusic;
    _nextBtn.enabled = !hasNextMusic;
    if (hasNextMusic) {
        [_nextBtn setImage:[UIImage imageNamed:@"playnext"] forState:UIControlStateNormal];
    }else{
        NSLog(@"12443");
        [_nextBtn setImage:[UIImage imageNamed:@"noplaynext"] forState:UIControlStateNormal];
    }
}
- (void)setHasLastMusic:(BOOL)hasLastMusic {
    _hasLastMusic = hasLastMusic;
    _formerBtn.enabled = !hasLastMusic;
    if (hasLastMusic) {
           [_formerBtn setImage:[UIImage imageNamed:@"playlast"] forState:UIControlStateNormal];
       }else{
           [_formerBtn setImage:[UIImage imageNamed:@"noplaylast"] forState:UIControlStateNormal];
       }
}

#pragma mark - getters

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
