您的位置:首页 > 移动开发 > IOS开发

iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环)

2015-10-05 23:32 633 查看
首先我在这里讲一下我的整个播放器的思路:

首先是一个歌曲的列表,我把数据请求放在了一个单例里面,方便以后获取每首歌曲对应的model, 我给AVPlayer的播放也放在了一个单例里面,有播放开始,停止,等方法吗,我通过单例的代理方法将当前的播放时间传给播放时的控制器,这样控制器就可以根据传过去的当前时间给界面赋值了,比如播放界面的当前时间,以及当前的歌词等.

实现播放模式的思路:

1.通过点击按钮 弹出来一个下弹窗 可以选择播放模式 声明一个全局变量 不同的点击全局变量的值改变 全局变量默认的播放模式是列表循环

2.播放音乐时给播放添加计时器每隔0/1秒就要响应一次 通过代理方法传给控制器当前播放时间

3.在控制器的代理方法中 根据传过来的时间与当前歌曲的总时间对比,如果相等说明这首歌结束了,就调用歌曲结束的方法.

4.在音乐播放完毕的时候调用方法 根据不同的全局变量 实现不同的操作

第一步 实现button的点击方法 通过点击不同的下弹窗的值改变全局变量,记录选择的模式

/ 模式typeButton的点击方法的实现
- (void)actionTypeButton:(UIButton *)typeButton {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择模式" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
// 添加顺序播放按钮
UIAlertAction *serialAction = [UIAlertAction actionWithTitle:@"顺序播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
// 给定义的全局变量赋值
self.typeCount = 0;
}];
// 添加随机播放按钮
UIAlertAction *ArcAction = [UIAlertAction actionWithTitle:@"随机播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
self.typeCount = 1;
}];
// 添加重复播放按钮
UIAlertAction *repeatAction = [UIAlertAction actionWithTitle:@"重复播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {
self.typeCount = 2;
}];
[alertController addAction:serialAction];
[alertController addAction:ArcAction];
[alertController addAction:repeatAction];

[self presentViewController:alertController animated:YES completion:nil];
}


第二步:给播放添加计时器每隔0/1秒就要响应一次 通过代理方法传给控制器当前播放时间

// 开始播放
- (void)musicPlay {
self.isPlaying = YES; // 当前的播放状态
[self.player play]; // AVPlayer开始播放
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playingAction) userInfo:nil repeats:YES]; // 开始计时器 调用播放的响应方法
}

#pragma mark -播放过程中执行
- (void)playingAction {
// 取到当前播放时间的秒数
CGFloat time = self.player.currentTime.value / self.player.currentTime.timescale;

// 代理方法 将获取到的时间 传递到控制器
if (self.delegate && [self.delegate performSelector:@selector(playingWithProgress:)]) {
[_delegate playingWithProgress:time];
}

}


第三步:在控制器的代理方法中 根据传过来的时间与当前歌曲的总时间对比,如果相等说明这首歌结束了,就调用歌曲结束的方法.

#pragma mark -- 实现代理方法
- (void)playingWithProgress:(CGFloat)progress {
// progress 当前歌曲播放到的时间
// self.model.duration 当前歌曲的总时间
NSInteger second = self.model.duration / 1000;
if (progress == second) {
[self musicEnd];
}
}


第四步:得到歌曲的进度的值 当播放完毕的时候 做不同的操作

#pragma mark --音乐结束后 不同模式下的反应--
- (void)musicEnd {
// progress 当前歌曲播放到的时间
// self.model.duration 当前歌曲的总时间
NSInteger second = self.model.duration / 1000;
if (progress == second) {
switch (self.typeCount) {
case 0:
{
// 当选择列表循环时候的操作
// actionDownButton: 下面有方法的实现
[self performSelector:@selector(actionDownButton:) withObject:nil];
break;
}
case 1:
{
// 当选择随机播放是的操作
NSInteger num = [[RootTableViewManager shareManager] getDataArrayCount];
self.index = arc4random() % (num + 1);
// 更改了index 就相当于改变了model 更改了数据  所以要刷新界面
[self Valuation]; // 该方法是更改界面 给AVPlayer更换playerItem(就是穿进去新的MP3的url) 播放音乐
break;
}
case 2:
{
// 当选择循环播放时的操作 只要更改界面就可以了
[self Valuation];
break;
}

default:
break;
}
}

}


// actionDownButton:方法的实现

// 下一首的实现方法
- (void)actionDownButton:(UIButton *)downButton {
self.index ++;
NSInteger num = [[RootTableViewManager shareManager] getDataArrayCount];
// 当时最后一首的时候 跳到最前面
if (self.index > num) {
self.index = 0;
}
[self Valuation];

}


// 界面的赋值是根据model的 线面是model的实现

/ 重写model的get方法
- (Model *)model {
// 根据当前的index 选中对应index歌曲的model 成为当前播放的数据源
odel *model =  [[RootTableViewManager shareManager] getModelAtIndex:self.index];
return model;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios 音乐 AVPlayer