您的位置:首页 > 大数据 > 物联网

AFSoundManager 音乐播放 逻辑

2016-07-08 00:00 766 查看
摘要: AFSoundManager使用了AudioToolbox和AVFoundation框架来服务音频。

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

[code=language-objectivec]#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

[code=language-objectivec]//

#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。

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

playback的加载音频

[code=language-objectivec]- (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

[code=language-objectivec]- (void)play{
[[YXPlayerManager shareManager].playback play];
}
- (void)pause{
[[YXPlayerManager shareManager].playback pause];
}

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

[code=language-objectivec]- (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];
}

}

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

[code=language-objectivec]-(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];

}

一个简单的播放器。写的有点糙,如果有问题希望可以指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息