AFSoundManager 音乐播放 逻辑

首先先导入这个AFSoundManager库,这个播放器,当前只能播放一首歌。可以把这个封装成一个单例。

#import <Foundation/Foundation.h>
#import "AFSoundManager.h"
@interface YXPlayerManager : NSObject
@property(nonatomic,strong)AFSoundQueue *queue;
@property(nonatomic,strong)AFSoundPlayback *playback;
@property(nonatomic,assign)int count;
@property (nonatomic,strong)UISlider * slider;
@property (nonatomic,strong)UILabel * starTime;
@property (nonatomic,strong)UILabel * endTime;
@property (nonatomic,strong)UIButton *controlBtn;
@property (nonatomic,copy)NSString * audioContenturl;
@property (nonatomic,assign)NSInteger status;
@property (nonatomic,strong)NSNumber * listId;//用来标识当前播放的id


//关于下载
@property (nonatomic,strong)AFHTTPRequestOperation * operation;
@property (nonatomic,strong)NSMutableArray * listIdArr;
@property (nonatomic,strong)NSMutableDictionary * operationDic;
+(YXPlayerManager *)shareManager;
@end

以上是单例的.h

//

#import "YXPlayerManager.h"

@implementation YXPlayerManager
+(YXPlayerManager *)shareManager
{
    static YXPlayerManager *manager= nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        manager = [[YXPlayerManager alloc]init];
        manager.queue =[[AFSoundQueue alloc]initWithItems:[NSMutableArray array]];
        AFSoundItem * soundItem = [[AFSoundItem alloc]initWithStreamingURL:[NSURL URLWithString:@""]];
        manager.playback = [[AFSoundPlayback alloc]initWithItem:soundItem];
        manager.operationDic = [NSMutableDictionary dictionary];
        manager.operation =[[AFHTTPRequestOperation alloc]init];//下载

    });
    return manager;
}

@end

以上是单例的.m

 

 

先创建一个播放的对象,我是用的playback。

        NSString * audioStr = voiceRcordList[indexPath.row];//当前点击cell对应的音频url
        soundItem =[[AFSoundItem alloc]initWithStreamingURL:[NSURL URLWithString:audioStr]];
        [YXPlayerManager shareManager].playback = [[AFSoundPlayback alloc]initWithItem:soundItem];
    }

playback的加载音频

- (void)listen{
    [[YXPlayerManager shareManager].playback listenFeedbackUpdatesWithBlock:^(AFSoundItem *item) {
        soundItem = item;
        duration = item.duration;//保存好总的时长
        WCLog(@"yanxue = %ld",(long)item.timePlayed);
        [[YXPlayerManager shareManager].slider setValue:item.timePlayed animated:YES];
        [YXPlayerManager shareManager].starTime.text = [self timeFormatted:(int)item.timePlayed];
        NSDictionary * dic = [[YXPlayerManager shareManager].playback statusDictionary];
        [YXPlayerManager shareManager].slider.maximumValue = [[dic objectForKey:@"duration"]intValue];
        [YXPlayerManager shareManager].endTime.text = [self timeFormatted:[YXPlayerManager shareManager].slider.maximumValue];
    } andFinishedBlock:^{
        [[YXPlayerManager shareManager].controlBtn setImage:[UIImage imageNamed:@"start"] forState:UIControlStateNormal];
        [YXPlayerManager shareManager].starTime.text = @"00:00";
        [[YXPlayerManager shareManager].slider setValue:0 animated:YES];
    }];
}

 

playback的play 和pause

- (void)play{
    [[YXPlayerManager shareManager].playback play];
}
- (void)pause{
    [[YXPlayerManager shareManager].playback pause];
}

最后是点击播放之后的逻辑

- (void)netWorkPlayer:(NSIndexPath *)indexPath
{
    VoiceRecordDetailCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [self syncCurrentUI2YXPlayer:cell];
    //处理当前正在播放的音频
    if (indexPath.row == indexPathRow) {
        if ([YXPlayerManager shareManager].playback.status == AFSoundStatusNotStarted  || [YXPlayerManager shareManager].playback.status == AFSoundStatusFinished) {
            [self listen];
            [cell.controlBtn setImage:[UIImage imageNamed:@"voice_pause_img"] forState:UIControlStateNormal];
            [self play];
            indexPathRow = indexPath.row;
         }
        else if ([YXPlayerManager shareManager].playback.status == AFSoundStatusPlaying){
            [self pause];
            [cell.controlBtn setImage:[UIImage imageNamed:@"voice_play_img"] forState:UIControlStateNormal];
        }
        else if ([YXPlayerManager shareManager].playback.status == AFSoundStatusPaused){
            [cell.controlBtn setImage:[UIImage imageNamed:@"voice_pause_img"] forState:UIControlStateNormal];
            [self play];
        }
    }
    //重新点击了一个新的音频
    else{
        indexPathRow = indexPath.row;
        [cell.controlBtn setImage:[UIImage imageNamed:@"voice_pause_img"] forState:UIControlStateNormal];
        [self listen];
        [self play];
    }
    
    
}

还有一些小细节,比如说进度条的更新,,拖动进度条要跟随时间去播放。当前的时间 ,以及整首歌的总时长 。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *CellIdentifier = @"VoiceRecordDetailCell";
    [tableView registerClass:[VoiceRecordDetailCell class] forCellReuseIdentifier:CellIdentifier];
    VoiceRecordDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        VoiceRecordDetailCell  * cell = [[VoiceRecordDetailCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.backgroundColor = [UIColor whiteColor];
    }
    [cell.slider addTarget:self action:@selector(startDrag:) forControlEvents:UIControlEventTouchDown];
    [cell.slider addTarget:self action:@selector(updateThumb:) forControlEvents:UIControlEventValueChanged];
    [cell.slider addTarget:self action:@selector(endDrag:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
    [cell.controlBtn addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消cell的选中效果
    return cell;
    
}
// 持续动作
- (void) updateThumb: (UISlider *) aSlider
{
    VoiceRecordDetailCell * cell;
    NSIndexPath * indexPath;
    cell = (VoiceRecordDetailCell *)aSlider.superview.superview.superview;
    indexPath = [self.tableView indexPathForCell:cell];
    WCLog(@"%ld",(long)indexPath.row);
    [cell.slider setValue:aSlider.value animated:YES];
    cell.starTimeLabel.text = [self timeFormatted:(int)aSlider.value];
}
// 开始
- (void) startDrag: (UISlider *) aSlider
{
    VoiceRecordDetailCell * cell;
    NSIndexPath * indexPath;
    cell = (VoiceRecordDetailCell *)aSlider.superview.superview.superview;
    indexPath = [self.tableView indexPathForCell:cell];
    [cell.slider setValue:aSlider.value animated:YES];
    cell.starTimeLabel.text = [self timeFormatted:(int)aSlider.value];
    [cell.controlBtn setImage:[UIImage imageNamed:@"voice_play_img"] forState:UIControlStateNormal];
}
//结束
- (void) endDrag: (UISlider *) aSlider
{
    VoiceRecordDetailCell * cell;
    NSIndexPath * indexPath;
    cell = (VoiceRecordDetailCell *)aSlider.superview.superview.superview;
    indexPath = [self.tableView indexPathForCell:cell];
    [[YXPlayerManager shareManager].playback playAtSecond:aSlider.value];
    [cell.controlBtn setImage:[UIImage imageNamed:@"voice_pause_img"] forState:UIControlStateNormal];
}
//播放的按钮
- (void)playAudio:(UIButton *)button{

    VoiceRecordDetailCell * cell;
    NSIndexPath * indexPath;
    cell = (VoiceRecordDetailCell *)button.superview.superview;
    indexPath = [self.tableView indexPathForCell:cell];
    if (indexPath == nil) {
        cell = (VoiceRecordDetailCell *)button.superview.superview.superview;
        indexPath = [self.tableView indexPathForCell:cell];
    }
    [self loadNetWorkPlayer:indexPath];

}

一个简单的播放器。写的有点糙,如果有问题希望可以指正。

转载于:https://my.oschina.net/yanxue1825/blog/708734

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值