JGMusicManager.m 5.54 KB
//
//  JGMusicManager.m
//  视频音频-Demo
//
//  Created by stkcctv on 16/8/25.
//  Copyright © 2016年 stkcctv. All rights reserved.
//

#import "JGMusicManager.h"

@implementation JGMusicManager

+ (instancetype)shareMusicManager {
    
    static JGMusicManager *musicManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        musicManager = [[JGMusicManager alloc] init];
        [musicManager remoteControlEventHandler];
    });
    return musicManager;
}

- (instancetype)init {
    if (self = [super init]) {
        _player = [[AVPlayer alloc] init];
    }
    return self;
}
- (void)playStop {
    [self playerPause];
}
//播放或暂停
- (void)playAndPause {
    if (self.isPlay) {
        [self playerPause];
    } else {
        [self playerPlay];
    }
}
//播放
- (void)playerPlay {
    [_player play];
    _isPlay = YES;
}
//暂停
- (void)playerPause {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pauseVocieNotification" object:nil];
    [_player pause];
    _isPlay = NO;
}
//播放上一首
- (void)playPrevious {
    if (self.index == 0) {
        self.index = self.musicArray.count - 1;
    }else {
        self.index--;
    }
}
//播放下一首
- (void)playNext {
    if (self.index == self.musicArray.count - 1) {
        self.index = 0;
    }else {
        self.index++;
    }
}

- (void)playerVolumeWithVolumeFloat:(CGFloat)volumeFloat {
    self.player.volume = volumeFloat;
}

- (void)playerProgressWithProgressFloat:(CGFloat)progressFloat {
    [self.player seekToTime:CMTimeMakeWithSeconds(progressFloat, 1) completionHandler:^(BOOL finished) {
        [self playerPlay];
    }];
}

- (void)replaceItemWithDictionary:(NSDictionary *)dictionary {
    [self setLockingInfo:dictionary];
    NSString *urlString = dictionary[@"videoUrl"];
    urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:urlString]];
    [self playerPause];
    [self.player replaceCurrentItemWithPlayerItem:item];
    [self playerPlay];
    [AlertViewTool hideHUDView];
}

//设置锁屏信息
- (void)setLockingInfo:(NSDictionary *)dictionary
{
    
    Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
    
    if (playingInfoCenter) {
        //数据信息
        NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
        
        //        //图片
        //        MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageWithUrlString:model.icon]];
        //        [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
        //
        //        //当前播放时间
        //        [songInfo setObject:[NSNumber numberWithDouble:[[[HWMusicTool shareMusicTool] Player] currentPlaybackTime]] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
        //        //速率
        //        [songInfo setObject:[NSNumber numberWithFloat:1.0f] forKey:MPNowPlayingInfoPropertyPlaybackRate];
        //        //剩余时长
        //        [songInfo setObject:[NSNumber numberWithDouble:[[[HWMusicTool shareMusicTool] Player] duration]] forKey:MPMediaItemPropertyPlaybackDuration];
        
        //设置标题
        [songInfo setObject:dictionary[@"cmsColumnName"] ? dictionary[@"cmsColumnName"] : @"" forKey:MPMediaItemPropertyTitle];
        
        //设置副标题
        [songInfo setObject:dictionary[@"title"] ? dictionary[@"title"] : @"" forKey:MPMediaItemPropertyArtist];
        
        //设置音频数据信息
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
    }
}

#pragma mark - private method
// 在需要处理远程控制事件的具体控制器或其它类中实现
- (void)remoteControlEventHandler {
    // 直接使用sharedCommandCenter来获取MPRemoteCommandCenter的shared实例
    MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
    
    // 播放, 暂停, 上下曲的命令默认都是启用状态, 即enabled默认为YES
    // 播放
    @try {
    
        [commandCenter.playCommand addTarget:self action:@selector(playAction:)];//playAction:)
    // 暂停
        [commandCenter.pauseCommand addTarget:self action:@selector(pauseAction:)];
        
        // 上一曲
        [commandCenter.previousTrackCommand addTarget:self action:@selector(previousTrackAction:)];
        // 下一曲
        [commandCenter.nextTrackCommand addTarget:self action:@selector(nextTrackAction:)];
        
        // 启用耳机的播放/暂停命令 (耳机上的播放按钮触发的命令)
        commandCenter.togglePlayPauseCommand.enabled = YES;
        // 为耳机的按钮操作添加相关的响应事件
        [commandCenter.togglePlayPauseCommand addTarget:self action:@selector(playOrPauseAction:)];
       }
    
       @catch (NSException *exception) {
    
           NSLog(@"exception = %@", exception);
    
       }
    
    @finally {
        
    }
    
}

- (void)playAction:(MPRemoteCommandCenter *)sender {
    [self playerPlay];
}

- (void)pauseAction:(MPRemoteCommandCenter *)sender {
    [self playerPause];
}

- (void)previousTrackAction:(MPRemoteCommandCenter *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(lastMusic)]) {
        [self.delegate lastMusic];
    }
}

- (void)nextTrackAction:(MPRemoteCommandCenter *)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(NextMusic)]) {
        [self.delegate NextMusic];
    }
}

- (void)playOrPauseAction:(MPRemoteCommandCenter *)sender {
    [self playAndPause];
}

@end