音乐播放器的简单入门使用

实现播放模式的思路:
1.通过点击按钮 弹出来一个下弹窗 可以选择播放模式 声明一个全局变量 不同的点击全局变量的值改变 全局变量默认的播放模式是列表循环
2.在音乐播放完毕的时候调用方法 根据不同的全局变量 实现不同的操作

第一步 实现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];
}

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

#pragma mark --音乐结束后 不同模式下的反应--
- (void)musicEnd:(CGFloat)progress
{
// 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];
                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
{
   odel *model =  [[RootTableViewManager shareManager] getModelAtIndex:self.index];
    return model;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值