_itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }

来源:互联网 发布:spring apache shiro 编辑:程序博客网 时间:2024/04/29 14:40

近日在做一个基于MPMoviePlayerViewController播放m3u8格式的广播APP时候,引发一个全球的搜索,在简单的代码播放测试成功后,

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];  

做了播放状态的监测,代码如下:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];  
  2.         [notificationCenter addObserver:self  
  3.                                selector:@selector(moviePlayerPreloadDidFinish:)  
  4.                                    name:  
  5.          MPMediaPlaybackIsPreparedToPlayDidChangeNotification  
  6.                                  object:player];  

可以从接口API文件中找到这个已经加载成功后的通知,非常简单即可完成,但是根据程序员的0 1 思想,有成功,必然要有失败,于是让手机处于飞行模式,于是检测到爆出错误:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. _itemFailedToPlayToEnd: { kind = 1new = 2; old = 0; }  

但是找遍系统API也没有找到一个监测到失败加载的通知,于是开始有目的的Google和百度,甚至stackoverflow,但是都是疑问和我类似,始终没有发现解决方案,最后终于在一个角落找到一个解决方案,根据代码测试,发现非常靠谱,代码如下:

首先,设置通知监听:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. [notificationCenter addObserver:self  
  2.                                selector:@selector(videoHasFinishedPlaying:)  
  3.                                    name:MPMoviePlayerPlaybackDidFinishNotification  
  4.                                  object:player];  


然后在监听回调函数中做如下判断:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. - (void)videoHasFinishedPlaying:(NSNotification *)paramNotification{  
  2.     /* Find out what the reason was for the player to stop */  
  3.     NSNumber *reason =  
  4.     [paramNotification.userInfo  
  5.      valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];  
  6.     if (reason != nil){  
  7.         NSInteger reasonAsInteger = [reason integerValue];  
  8.         switch (reasonAsInteger){  
  9.             case MPMovieFinishReasonPlaybackEnded:{  
  10.                 /* The movie ended normally */  
  11.                 break; }  
  12.             case MPMovieFinishReasonPlaybackError:{  
  13.                 /* An error happened and the movie ended */  
  14.                 break;  
  15.             }  
  16.             case MPMovieFinishReasonUserExited:{  
  17.                 /* The user exited the player */  
  18.                 break;  
  19.             }   
  20.         }  
  21.         NSLog(@"Finish Reason = %ld", (long)reasonAsInteger);  
  22.     } /* if (reason != nil){ */   
  23. }   

通过以上判断即可得到加载失败的原因,可以处理 _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; } 这种错误信息,最后根据自己的需要完成用户提示等功能,至此实现了 0 1 功能。
0 0
原创粉丝点击