.h文件
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
#import "RadioDetailListList.h"//对应数据源的model(存放音频资源)
@interface RadioPlayManager : NSObject
@property(nonatomic,assign) BOOL isPlay;//播放状态
@property(nonatomic,assign) NSInteger playIndex;//播放歌曲索引
@property(nonatomic,copy) NSMutableArray *musicArray;//播放列表
@property(nonatomic,assign) float currentTime;//当前时间
@property(nonatomic,assign) float totalTime;//总时间
@property(nonatomic,strong) AVPlayer *avPlayer;
+ (instancetype)defaultManager;
-(void)play; //播放
-(void)pause; //暂停
-(void)seekTotime:(float)time; //跳到指定位置播放
-(void)lastMusic;//上一首
-(void)nextMusic;//下一首
-(void)changeMusicWithIndex:(NSInteger)index;//切换歌曲
-(void)playerDidFinish;//播放完成
@end
.m文件
#import "RadioPlayManager.h"
@implementation RadioPlayManager
//单列的声明
+ (instancetype)defaultManager{
static RadioPlayManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[RadioPlayManager alloc]init];
});
return manager;
}
//重写init方法
-(instancetype)init{
self = [super init];
if (self) {
_musicArray = [NSMutableArray array];
}
return self;
}
//通过播放列表数组 初始化_avPlayer(通过AVPlayerItem来初始化)
-(void)setMusicArray:(NSMutableArray *)musicArray{
[_musicArray removeAllObjects];
_musicArray = musicArray;
//通过索引拿到model
RadioDetailListList *listmodel = _musicArray[_playIndex];
AVPlayerItem *avPlayerItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:listmodel.musicUrl]];
if (_avPlayer) {
//切换的时候 AVPlayer要用到AVPlayerItem来切换
[_avPlayer replaceCurrentItemWithPlayerItem:avPlayerItem];
}
else{
//初始化的时候 AVPlayer要用到AVPlayerItem来初始化
_avPlayer = [[AVPlayer alloc]initWithPlayerItem:avPlayerItem];
}
}
//当前时间
-(float)currentTime
{
// 当前基数为零
if (_avPlayer.currentItem.timebase == 0) {
return 0;
}
//value/timescale = seconds.
//当前时间的秒
return _avPlayer.currentTime.value / _avPlayer.currentTime.timescale;
}
//总时间
-(float)totalTime
{
if (_avPlayer.currentItem.duration.timescale == 0) {
return 0;
}
//当前item的duration 持续的时间
return _avPlayer.currentItem.duration.value / _avPlayer
.currentItem.duration.timescale;
}
#pragma mark - 播放控制
//播放
-(void)play{
[_avPlayer play];
_isPlay = YES;
}
//关闭
-(void)pause
{
[_avPlayer pause];
_isPlay = NO;
}
//秒
-(void)seekTotime:(float)time
{
[self pause];
CMTime newtime = _avPlayer.currentTime;
newtime.value = newtime.timescale * time;
[_avPlayer seekToTime:newtime];
[self play];
}
//切歌
-(void)changeMusicWithIndex:(NSInteger)index
{
_playIndex = index;
RadioDetailListList *listmodel = _musicArray[_playIndex];
//AVPlayerItem 初始化 要用到一个URL
AVPlayerItem *avPlayerItem = [[AVPlayerItem alloc]initWithURL:[NSURL URLWithString:listmodel.musicUrl]];
[_avPlayer replaceCurrentItemWithPlayerItem:avPlayerItem];
[self play];
}
//上一首
-(void)lastMusic
{
_playIndex --;
if (_playIndex<0) {
_playIndex = _musicArray.count -1;
}
[self changeMusicWithIndex:_playIndex];
}
//下一首
-(void)nextMusic{
_playIndex ++;
_playIndex = _playIndex % _musicArray.count;
[self changeMusicWithIndex:_playIndex];
}
//播放完毕
-(void)playerDidFinish
{
[self nextMusic];
}
@end