statues

更新时间:2022-11-25 14:21:20 阅读: 评论:0


2022年11月25日发(作者:dove of peace)

AVPlayer播放⽹络⾳频

使⽤AVPlayer播放⽹络⾳频简单易⾏,但是在使⽤的时候我们需要很多时间的监控处理,这⾥就需要分装⼀下,好供我们使⽤,在不同的地⽅使⽤,省去

了繁琐的事件监听出,KVO处理不好容易造成多移除崩溃,为移除崩溃,封装到⼀个类中,统⼀处理,简单⽅便,

<创建⼀个播放类⽂件>

**PlayerSource.h**

#import

#import**导⼊框架**

<*顶⼀个枚举类型,记录播放的状态*>

typedefNS_ENUM(NSUInteger,cq_audioPayerState){

cq_audioPayerStateNormal=0,/**<未播放状态*/

cq_audioPayerStatePlaying=2,/**<正在播放*/

cq_audioPayerStateCancle=3,/**<播放被取消*/

};

<*定义代理⽅法将播放状态传到出去,⽅便事件的处理*>

@protocolcqAudioPlayerDelegate

-(void)audioPlayerStatuesDidChanged:(cq_audioPayerState)audioPlayerindex:(NSInteger)index;

@end

@interfacePlayerSource:NSObject

@property(nonatomic,copy)NSString*URlString;

@property(nonatomic,assign)NSUIntegerindex;

@property(nonatomic,weak)iddelegate;

@property(nonatomic,strong)AVPlayer*audioPlayer;

@property(nonatomic,assign)cq_audioPayerStateaudioStatues;

//记录是否在播放,⽅便记录是否移除了KVO

@property(nonatomic,assign)BOOLisPlaying;

+(instancetype)sharePlayer;

/**

播放事件

@paramURl资源链接

@paramindex记录当前播放的原位置//例如点击tableviewCell的⼀个index为1第5个cell为5

*/

-(void)playAudioWithURLString:(NSString*)URlatindex:(NSUInteger)index;

/**

直接播放事件

@paramURlstring资源链接

*/

-(void)playAudioWithWebUrlString:(NSString*)URlstring;

/**

停⽌事件

*/

-(void)stopAudioPlayer;

@end

/**

这⾥使⽤单例模式⽅便⽅法的调⽤

*/

+(instancetype)sharePlayer{

staticdispatch_once_tonceToken;

staticidshareInstance;

dispatch_once(&onceToken,^{

shareInstance=[[lfalloc]init];

});

returnshareInstance;

}

-(void)playAudioWithURLString:(NSString*)URlatindex:(NSUInteger)index

{

if(!URl){

return;

}

if([ingisEqualToString:URl]&&_index==index){

[lfstopAudioPlayer];

[lftAudioStatues:cq_audioPayerStateCancle];

ed=NO;

return;

}

ing=URl;

=index;

dispatch_async(dispatch_get_main_queue(),^{

[lfplayAudioWithWebUrlString:URl];

});

}

-(void)playAudioWithWebUrlString:(NSString*)URlstring

{

if(_isPlaying){//如果在播放表⽰KVO未被移除,移除KVO

[_audioPlayerremoveObrver:lfforKeyPath:@"status"];

_isPlaying=NO;

}

AVAst*ast=[AVURLAstURLAstWithURL:[NSURLURLWithString:URlstring]options:nil];

AVPlayerItem*playerItem=[AVPlayerItemplayerItemWithAst:ast];

_audioPlayer=[AVPlayerplayerWithPlayerItem:playerItem];

[[AVAudioSessionsharedInstance]tCategory:AVAudioSessionCategoryPlaybackwithOptions:AVAudioSessionCategoryOptionAllowBluetootherror:nil];

[[AVAudioSessionsharedInstance]tActive:YESerror:nil];

//添加监听博播状态的KVO

[_audioPlayeraddObrver:lfforKeyPath:@"status"options:NSKeyValueObrvingOptionNewcontext:nil];

[_audioPlayerplay];

_isPlaying=YES;//表⽰播放,添加了KVO

[lfaddNotification];

}

//

-(void)tAudioStatues:(cq_audioPayerState)audioStatues

{

_audioStatues=audioStatues;

if(te&&[terespondsToSelector:@lector(audioPlayerStatuesDidChanged:index:)]){

[teaudioPlayerStatuesDidChanged:audioStatuesindex:];

}

if(_audioStatues==cq_audioPayerStateCancle||audioStatues==cq_audioPayerStateNormal){

_URlString=nil;

_index=NSIntegerMax;//播放完成将数值改变,防⽌下次播放时判断是同⼀个在播放的资源

tAudioStatues:cq_audioPayerStateCancle];

}

}

//

<*添加KVO*>

-(void)addNotification

{

[[NSNotificationCenterdefaultCenter]addObrver:lf

lector:@lector(playbackFinished:)

name:AVPlayerItemDidPlayToEndTimeNotification

object:_tItem];

}

<*监测是否⾃动播放完成*>

-(void)playbackFinished:(NSNotification*)notice

{

[lfstopAudioFinish];

}

//事件的监听处理

-(void)obrveValueForKeyPath:(NSString*)keyPathofObject:(id)objectchange:(NSDictionary*)changecontext:(void*)context

{

if([keyPathisEqualToString:@"status"]){

AVPlayerStatusstatus=[[changeobjectForKey:@"new"]intValue];

if(status==AVPlayerStatusReadyToPlay){

ed=YES;

[lftAudioStatues:cq_audioPayerStatePlaying];

[_timerinvalidate];

_timer=[NSTimerscheduledTimerWithTimeInterval:1target:lflector:@lector(timeAction:)urInfo:nilrepeats:YES];

}elif(status==AVPlayerStatusUnknown)

{

ReachabilityStatusstatues=[GLobalRealReachabilitycurrentReachabilityStatus];

if(statues==RealStatusNotReachable){

[lftAudioStatues:cq_audioPayerStateCancle];

[lfstopAudioPlayer];

_hud=[MBProgressHUDshowHUDAddedTo:[UIApplicationsharedApplication].keyWindowanimated:YES];

_=MBProgressHUDModeText;

_ext=@"⽹络连接有误";

_=10.0f;//提⽰框的⼤⼩

[_hudcustomView];

_FromSuperViewOnHide=YES;//隐藏之后删除view

[_hudhide:YESafterDelay:0.8];//多久后隐藏

return;

}el

{

}

}elif(status==AVPlayerStatusFailed)

{

[lftAudioStatues:cq_audioPayerStateNormal];

[lfstopAudioPlayer];

_hud=[MBProgressHUDshowHUDAddedTo:[UIApplicationsharedApplication].keyWindowanimated:YES];

_=MBProgressHUDModeText;

_ext=@"播放有误";

_=10.0f;//提⽰框的⼤⼩

[_hudcustomView];

_FromSuperViewOnHide=YES;//隐藏之后删除view

[_hudhide:YESafterDelay:0.5];//多久后隐藏

}

}elif([keyPathisEqualToString:@"loadedTimeRanges"]){

}

}

<*中途暂停的播放事件*>

-(void)stopAudioPlayer

{

if(_audioPlayer){

if(_==AVPlayerStatusReadyToPlay){

[_audioPlayerpau];

if(_isPlaying){

[_audioPlayerremoveObrver:lfforKeyPath:@"status"];

_isPlaying=NO;

}

[_timertFireDate:[NSDatedistantFuture]];

ed=NO;

[[PlayerSourcesharePlayer]tAudioStatues:cq_audioPayerStateCancle];

}

}

}

<*正常播放完成的暂停事件*>

-(void)stopAudioFinish

{

if(_audioPlayer){

if(_==AVPlayerStatusReadyToPlay){

[_audioPlayerpau];

_isfinish=YES;

[[PlayerSourcesharePlayer]tAudioStatues:cq_audioPayerStateNormal];

ed=NO;

if(_isPlaying){

[_audioPlayerremoveObrver:lfforKeyPath:@"status"];

_isPlaying=NO;

}

_isShowView=NO;

[ewremoveFromSuperview];

ew=nil;

}

}

}

<以上便是播放的内容,下⾯介绍如何使⽤>

先介绍播放点击tableViewCell的播放事件

pragmamark====点击播放声⾳=====

⾸先在需要播放的类这种的viewdidload中先遵循代理

[PlayerSourcesharePlayer].delegate=lf;

-(void)tapImgaeWithCell:(QuestionTableViewCell*)cell

{

if(IsLogin){

NSIndexPath*indexPath=[_QuestionTableindexPathForCell:cell];

QuestionStatues*statues=[_DataArrayobjectAtIndex:n];

if(==n){

//[celltVoicePlayStatues:cq_VoiceplayStatueCancle];

[[PlayerSourcesharePlayer]playAudioWithURLString:[detailPublicUrlstringByAppendingString:io]atindex:n];

=NSIntegerMax;

}el

{

[UIApplicationsharedApplication]workActivityIndicatorVisible=YES;

_IndexQuesID=;

[PlayerSourcesharePlayer].timeText=ioSec;

[[PlayerSourcesharePlayer]playAudioWithURLString:[detailPublicUrlstringByAppendingString:io]atindex:n];

[[PlayerSourcesharePlayer]tTitleLabel:letimeLabel:ioSec];

=n;

}

}el

{

[PublickResourceshowTipAlertViewWith:lftitle:@"未登录"message:@"是否⽴即登录"CancleTitle:@"取消"downTitle:@"⽴即登录"CancelButton:^{

}DownButton:^{

LoginViewController*log=[[LoginViewControlleralloc]init];

MKNavigationController*nav=[[MKNavigationControlleralloc]initWithRootViewController:log];

[lfprentViewController:navanimated:YEScompletion:nil];

}];

}

}

<*代理⽅法监听的处理*>

-(void)audioPlayerStatuesDidChanged:(cq_audioPayerState)audioPlayerindex:(NSInteger)index

{

if(index<_-1){

if(index<_-1){

NSIndexPath*indexPath=[NSIndexPathindexPathForRow:0inSection:index];

QuestionStatues*statues=[_DataArrayobjectAtIndex:index];

QuestionTableViewCell*voiceMessageCell=[_QuestionTablecellForRowAtIndexPath:indexPath];

VoiceImageStatuesvoiceStatues;

switch(audioPlayer){

cacq_audioPayerStateCancle:

voiceStatues=cq_VoiceplayStatueCancle;

_isPlaying=NO;

[UIApplicationsharedApplication]workActivityIndicatorVisible=NO;

break;

cacq_audioPayerStatePlaying:

voiceStatues=cq_VoiceplayStatuePlaying;

_isPlaying=YES;

[UIApplicationsharedApplication]workActivityIndicatorVisible=NO;

break;

cacq_audioPayerStateNormal:

_isPlaying=NO;

[lfaddClickNumberWithID:_IndexQuesID];

NSIntegerinter=[dintegerValue];

inter++;

d=[NSStringstringWithFormat:@"%ld",(long)inter];

[onTablereloadData];

voiceStatues=cq_VoiceplayStatueNormal;

[UIApplicationsharedApplication]workActivityIndicatorVisible=NO;

default:

break;

}

dispatch_async(dispatch_get_main_queue(),^{

[voiceMessageCelltVoicePlayStatues:voiceStatues];

});

}

}

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

<*cell.h中定义枚举类型*>

typedefNS_ENUM(NSUInteger,VoiceImageStatues){

cq_VoiceplayStatueNormal,/**<未播放状态*/

cq_VoiceplayStatuePlaying,/**<未播放状态*/

cq_VoiceplayStatueCancle,/**<未播放状态*/

};

<*定义代理⽅法*>

@protocolSelectVoiceImageDelegate

-(void)tapImgaeWithCell:(QuestionTableViewCell*)cell;

@end

<*在cell中声明枚举与代理⽅法*>

@property(nonatomic,weak)iddelegate;

@property(nonatomic,assign)VoiceImageStatuesVoicePlayStatues;

<介绍使⽤⽅法直接播放的⽅法>

<*同样的需要在类中遵循代理*>

[PlayerSourcesharePlayer].delegate=lf;

播放⽅法

[[PlayerSourcesharePlayer]playAudioWithWebUrlString:@""];

<*代理时间监听处理*>

-(void)audioPlayerStatuesDidChanged:(cq_audioPayerState)audioPlayerindex:(NSInteger)index

{

switch(audioPlayer){

cacq_audioPayerStateCancle:

[_StatuesImagestopAnimating];

[UIApplicationsharedApplication]workActivityIndicatorVisible=NO;

break;

cacq_audioPayerStatePlaying:

[_StatuesImagestartAnimating];

_isPlaying=YES;

[UIApplicationsharedApplication]workActivityIndicatorVisible=NO;

break;

cacq_audioPayerStateNormal:

[_StatuesImagestopAnimating];

[lfaddClickNumberWithID:_QueID];

[UIApplicationsharedApplication]workActivityIndicatorVisible=NO;

default:

break;

}

}

本文发布于:2022-11-25 14:21:20,感谢您对本站的认可!

本文链接:http://www.wtabcd.cn/fanwen/fan/90/18985.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:高薪工作
下一篇:diamond jubilee
标签:statues
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图