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

#import "ICMusicPlayManager.h"


@interface ICMusicPlayManager ()<XMTrackPlayerDelegate>{
    XMSDKPlayer *_sdkPlayer;
}
@end
@implementation ICMusicPlayManager
+ (instancetype)sharedManager {
    static ICMusicPlayManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _sdkPlayer = [XMSDKPlayer sharedPlayer];
        [_sdkPlayer setPlayMode:XMSDKPlayModeTrack];
        [_sdkPlayer setTrackPlayMode:XMTrackPlayerModeList];
        [_sdkPlayer setAutoNexTrack:YES];
        _sdkPlayer.trackPlayDelegate = self;
        [[XMSDKPlayer sharedPlayer] setVolume:0.5];
    }
    return self;
}
#pragma mark - XMTrackPlayerDelegate
//播放时被调用，频率为1s，告知当前播放进度和播放时间
- (void)XMTrackPlayNotifyProcess:(CGFloat)percent currentSecond:(NSUInteger)currentSecond
{
    NSLog(@"%s",__FUNCTION__);
    NSLog(@"percent:%.2lf",percent);
    
    if (self.KKPlayerPlayTimeChanged) {
        self.KKPlayerPlayTimeChanged(percent, currentSecond);
    }
    
}
//播放时被调用，告知当前播放器的缓冲进度
- (void)XMTrackPlayNotifyCacheProcess:(CGFloat)percent
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerBufferTimeChanged) {
        self.KKPlayerBufferTimeChanged(percent);
    }
}

#pragma mark - player state change
//播放列表结束时被调用
- (void)XMTrackPlayerDidPlaylistEnd
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidPlaylistEnd) {
        self.KKPlayerDidPlaylistEnd();
    }
}
//开始播放时调用
- (void)XMTrackPlayerDidStart
{
    NSLog(@"%s",__FUNCTION__);
    
    if (self.KKPlayerDidStart) {
        self.KKPlayerDidStart();
    }
}
//将要播放时被调用
- (void)XMTrackPlayerWillPlaying
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerWillPlaying) {
        self.KKPlayerWillPlaying();
    }
}
//已经播放时被调用
- (void)XMTrackPlayerDidPlaying
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidPlaying) {
        self.KKPlayerDidPlaying();
    }
}
//暂停时调用
- (void)XMTrackPlayerDidPaused
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidPaused) {
        self.KKPlayerDidPaused();
    }
}
//停止时调用
- (void)XMTrackPlayerDidStopped
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidStopped) {
        self.KKPlayerDidStopped();
    }
}
//结束播放时调用
- (void)XMTrackPlayerDidEnd
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidEnd) {
        self.KKPlayerDidEnd();
    }
}
//切换声音时调用
- (void)XMTrackPlayerDidChangeToTrack:(XMTrack *)track
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidChangeToTrack) {
        self.KKPlayerDidChangeToTrack(track);
    }
}
//播放失败时调用
- (void)XMTrackPlayerDidFailedToPlayTrack:(XMTrack *)track withError:(NSError *)error
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidFailedToPlayTrack) {
        self.KKPlayerDidFailedToPlayTrack(track,error);
    }
}
//播放失败时是否继续播放下一首
- (BOOL)XMTrackPlayerShouldContinueNextTrackWhenFailed:(XMTrack *)track
{
    NSLog(@"%s",__FUNCTION__);
    return NO;
}
//成功替换播放列表时调用
- (void)XMTrackPlayerDidReplacePlayList:(NSArray *)list
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidReplacePlayList) {
        self.KKPlayerDidReplacePlayList(list);
    }
}
//播放数据请求失败时调用，data.description是错误信息
- (void)XMTrackPlayerDidErrorWithType:(NSString *)type withData:(NSDictionary*)data
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidError) {
        self.KKPlayerDidError(type,data);
    }
}
//- (void)XMTrackPlayerDidErrorWithType:(NSInteger)type withData:(NSDictionary*)data;

//没有网络情况下播放器因缓冲已播完而停止播放时触发
- (void)XMTrackPlayerDidPausePlayForBadNetwork
{
    NSLog(@"%s",__FUNCTION__);
    if (self.KKPlayerDidPausePlayForBadNetwork) {
        self.KKPlayerDidPausePlayForBadNetwork();
    }
}


@end
