AVPlayer播放网络音频

使用AVPlayer播放网络音频简单易行,但是在使用的时候我们需要很多时间的监控处理,这里就需要分装一下,好供我们使用,在不同的地方使用,省去了繁琐的事件监听出,KVO处理不好容易造成多移除崩溃,为移除崩溃,封装到一个类中,统一处理,简单方便,
<创建一个播放类文件>

**PlayerSource.h**


#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h> **导入框架**
<*顶一个枚举类型,记录播放的状态*>
typedef NS_ENUM(NSUInteger, cq_audioPayerState){
    cq_audioPayerStateNormal = 0, /**< 未播放状态 */
    cq_audioPayerStatePlaying = 2, /**< 正在播放 */
    cq_audioPayerStateCancle = 3, /**< 播放被取消 */
};

<*定义代理方法将播放状态传到出去,方便事件的处理*>
@protocol cqAudioPlayerDelegate <NSObject>
- (void)audioPlayerStatuesDidChanged:(cq_audioPayerState)audioPlayer index:(NSInteger)index;
@end

@interface PlayerSource : NSObject
@property (nonatomic ,copy)NSString        *URlString;
@property (nonatomic, assign) NSUInteger    index;
@property (nonatomic, weak) id<cqAudioPlayerDelegate>delegate;
@property (nonatomic,strong)AVPlayer                    *audioPlayer;
@property (nonatomic ,assign)cq_audioPayerState         audioStatues;

//记录是否在播放,方便记录是否移除了KVO
@property (nonatomic ,assign)BOOL                       isPlaying;

+ (instancetype)sharePlayer;

/**
 播放事件

 @param URl 资源链接
 @param index 记录当前播放的原位置 //例如点击tableviewCell的一个 index为1 第5个cell为5
 */
- (void)playAudioWithURLString:(NSString *)URl atindex:(NSUInteger)index;
/**
 直接播放事件

 @param URlstring 资源链接
 */
- (void)playAudioWithWebUrlString:(NSString *)URlstring;
/**
 停止事件
 */
- (void)stopAudioPlayer;

@end

<PlayerSource>

/**
这里使用单例模式方便方法的调用
*/
+ (instancetype)sharePlayer{
    static dispatch_once_t onceToken;
    static id shareInstance;
    dispatch_once(&onceToken, ^{
        shareInstance = [[self alloc] init];
    });
    return shareInstance;
}
- (void)playAudioWithURLString:(NSString *)URl atindex:(NSUInteger)index
{
    if (!URl) {
        return;
    }
    
    if ([self.URlString isEqualToString:URl] && _index == index) {
        [self stopAudioPlayer];
        [self setAudioStatues:cq_audioPayerStateCancle];
        self.StatuesImage.selected = NO;
        return;
    }
    self.URlString = URl;
    self.index = index;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self playAudioWithWebUrlString:URl];
        });  
}
 - (void)playAudioWithWebUrlString:(NSString *)URlstring
{
    

    if (_isPlaying) {  //如果在播放 表示KVO未被移除,移除KVO
        [_audioPlayer removeObserver:self forKeyPath:@"status"];
        _isPlaying = NO;
    }
    
    AVAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:URlstring] options:nil];
    AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
    _audioPlayer = [AVPlayer playerWithPlayerItem:playerItem];
    
    [[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
    [[AVAudioSession sharedInstance]setActive:YES error:nil];
//添加监听博播状态的KVO
    [_audioPlayer addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
    [_audioPlayer play];
    _isPlaying = YES; //表示播放,添加了KVO
    [self addNotification];
}

//

-(void)setAudioStatues:(cq_audioPayerState)audioStatues
{
    _audioStatues = audioStatues;
    if (self.delegate && [self.delegate respondsToSelector:@selector(audioPlayerStatuesDidChanged:index:)]) {
        [self.delegate audioPlayerStatuesDidChanged:audioStatues index:self.index];
    }
    
    if (_audioStatues == cq_audioPayerStateCancle || audioStatues == cq_audioPayerStateNormal) {
        _URlString = nil;
        _index = NSIntegerMax; //播放完成将数值改变,防止下次播放时判断是同一个在播放的资源
setAudioStatues:cq_audioPayerStateCancle];
 
    }
    
}

//

<*添加KVO*>
- (void)addNotification
{
    [[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(playbackFinished:)
                                                name:AVPlayerItemDidPlayToEndTimeNotification
                                              object:_audioPlayer.currentItem];
}
<*监测是否自动播放完成*>
- (void)playbackFinished:(NSNotification *)notice
{   
    [self stopAudioFinish];   
}

//事件的监听处理

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"status"]) {
        AVPlayerStatus status= [[change objectForKey:@"new"]intValue];
        if(status==AVPlayerStatusReadyToPlay){
            self.StatuesImage.selected = YES;
            [self setAudioStatues:cq_audioPayerStatePlaying];
            [_timer invalidate];
            _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
        }else if (status == AVPlayerStatusUnknown)
        {
            ReachabilityStatus statues = [GLobalRealReachability currentReachabilityStatus];
            if (statues == RealStatusNotReachable) {
                [self setAudioStatues:cq_audioPayerStateCancle];
                [self stopAudioPlayer];
                _hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
                _hud.mode = MBProgressHUDModeText;
                _hud.labelText = @"网络连接有误";
                _hud.margin = 10.0f; //提示框的大小
                [_hud customView];
                _hud.removeFromSuperViewOnHide = YES; //隐藏之后删除view
                [_hud hide:YES afterDelay:0.8]; //多久后隐藏
                return;
            }else
            {
          
            }
            
        }else if (status == AVPlayerStatusFailed)
        {
            [self setAudioStatues:cq_audioPayerStateNormal];
            [self stopAudioPlayer];
            _hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
            _hud.mode = MBProgressHUDModeText;
            _hud.labelText = @"播放有误";
            _hud.margin = 10.0f; //提示框的大小
            [_hud customView];
            _hud.removeFromSuperViewOnHide = YES; //隐藏之后删除view
            [_hud hide:YES afterDelay:0.5]; //多久后隐藏
        }
    }else if([keyPath isEqualToString:@"loadedTimeRanges"]){
        
    }
}
<*中途暂停的播放事件*>
- (void)stopAudioPlayer
{
    if (_audioPlayer) {
        if (_audioPlayer.status == AVPlayerStatusReadyToPlay) {
            [_audioPlayer pause];
            if (_isPlaying) {
            [_audioPlayer removeObserver:self forKeyPath:@"status"];
                _isPlaying = NO;
            }
            [_timer setFireDate:[NSDate distantFuture]];
            self.StatuesImage.selected = NO;
            [[PlayerSource sharePlayer] setAudioStatues:cq_audioPayerStateCancle];
        }
    }
}
<*正常播放完成的暂停事件*>
- (void)stopAudioFinish
{
    if (_audioPlayer) {
        if (_audioPlayer.status == AVPlayerStatusReadyToPlay) {
            [_audioPlayer pause];
            _isfinish = YES;
            [[PlayerSource sharePlayer] setAudioStatues:cq_audioPayerStateNormal];
            self.StatuesImage.selected = NO;
            if (_isPlaying) {
            [_audioPlayer removeObserver:self forKeyPath:@"status"];
                 _isPlaying = NO;
            }
            _isShowView = NO;
            [self.backView removeFromSuperview];
            self.backView = nil;
            
        }
    }
}

<以上便是播放的内容,下面介绍如何使用>
先介绍播放点击tableViewCell的播放事件

pragma mark ====点击播放声音=====

首先在需要播放的类这种的viewdidload中先遵循代理
[PlayerSource sharePlayer].delegate = self;


- (void)tapImgaeWithCell:(QuestionTableViewCell *)cell
{
    if (IsLogin) {
        NSIndexPath *indexPath = [_QuestionTable indexPathForCell:cell];
        QuestionStatues *statues = [_DataArray objectAtIndex:indexPath.section];
        
        if (self.index == indexPath.section) {
            //        [cell setVoicePlayStatues:cq_VoiceplayStatueCancle];
            [[PlayerSource sharePlayer]playAudioWithURLString:[detailPublicUrl stringByAppendingString:statues.ansAudio] atindex:indexPath.section];
            self.index = NSIntegerMax;
            
        }else
        {
            [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
            _IndexQuesID = statues.queID;
            
            [PlayerSource sharePlayer].timeText = statues.ansAudioSec;
            [[PlayerSource sharePlayer]playAudioWithURLString:[detailPublicUrl stringByAppendingString:statues.ansAudio] atindex:indexPath.section];
            [[PlayerSource sharePlayer] setTitleLabel:statues.queTitle timeLabel:statues.ansAudioSec];
          
            self.index = indexPath.section;
        }
    }else
    {
        [PublickResource showTipAlertViewWith:self title:@"未登录" message:@"是否立即登录" CancleTitle:@"取消" downTitle:@"立即登录" CancelButton:^{
        } DownButton:^{
            LoginViewController *log = [[LoginViewController alloc]init];
            MKNavigationController *nav = [[MKNavigationController alloc]initWithRootViewController:log];
            [self presentViewController:nav animated:YES completion:nil];
        }];
        
    }  
}

<*代理方法监听的处理*>
-(void)audioPlayerStatuesDidChanged:(cq_audioPayerState)audioPlayer index:(NSInteger)index
{
    if (index < _DataArray.count - 1) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:index];
    QuestionStatues *statues = [_DataArray objectAtIndex:index];
    QuestionTableViewCell *voiceMessageCell = [_QuestionTable cellForRowAtIndexPath:indexPath];
    VoiceImageStatues voiceStatues;
    
    switch (audioPlayer) {
        case cq_audioPayerStateCancle:
            voiceStatues = cq_VoiceplayStatueCancle;
            _isPlaying = NO;
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            break;
        case cq_audioPayerStatePlaying:
            voiceStatues = cq_VoiceplayStatuePlaying;
            _isPlaying = YES;
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            break;
        case cq_audioPayerStateNormal:
            _isPlaying = NO;
            [self addClickNumberWithID:_IndexQuesID];
            NSInteger inter = [statues.clicked integerValue];
            inter++;
            statues.clicked = [NSString stringWithFormat:@"%ld",(long)inter];
            [self.QuestionTable reloadData];
            voiceStatues = cq_VoiceplayStatueNormal;
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        default:
            break;
    }
        dispatch_async(dispatch_get_main_queue(), ^{
        [voiceMessageCell setVoicePlayStatues:voiceStatues];
    });
        
    }
}


<*cell中代理与定义的实现*>

<*cell.h中定义枚举类型*>
typedef NS_ENUM(NSUInteger,VoiceImageStatues){
    cq_VoiceplayStatueNormal,/**< 未播放状态*/
    cq_VoiceplayStatuePlaying,/**< 未播放状态*/
    cq_VoiceplayStatueCancle,/**< 未播放状态*/
};

<*定义代理方法*>
@protocol SelectVoiceImageDelegate <NSObject>
- (void)tapImgaeWithCell:(QuestionTableViewCell *)cell;

@end

<*在cell中声明枚举与代理方法*>
@property (nonatomic ,weak)id<SelectVoiceImageDelegate>delegate;
@property (nonatomic ,assign)VoiceImageStatues   VoicePlayStatues;

<介绍使用方法直接播放的方法>

<*同样的需要在类中遵循代理*>
[PlayerSource sharePlayer].delegate = self;

播放方法
[[PlayerSource sharePlayer] playAudioWithWebUrlString: @""];


<*代理时间监听处理*>
- (void)audioPlayerStatuesDidChanged:(cq_audioPayerState)audioPlayer index:(NSInteger)index
{
    switch (audioPlayer) {
        case cq_audioPayerStateCancle:
            [_StatuesImage stopAnimating];
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            break;
        case cq_audioPayerStatePlaying:
            [_StatuesImage startAnimating];
            _isPlaying = YES;
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            break;
        case cq_audioPayerStateNormal:
            [_StatuesImage stopAnimating];
            [self addClickNumberWithID:_QueID];
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        default:
            break;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,723评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,485评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,998评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,323评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,355评论 5 374
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,079评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,389评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,019评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,519评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,971评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,100评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,738评论 4 324
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,293评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,289评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,517评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,547评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,834评论 2 345

推荐阅读更多精彩内容