MPMoviePlayerController 通知在 ios6 中不起作用
MPMoviePlayerController notification not working in ios6
我已经构建了一个演示,其中我只是播放实时视频 url,如果 url 无法正常工作,则 MPMoviePlayerController 会触发通知 MPMoviePlayerPlaybackDidFinishNotification MPMoviePlayerPlaybackDidFinishReasonUserInfoKey。这个东西在 ios7 和更高版本中完美运行,但是当我在 ios6 中使用同样的东西时,它不起作用。
我的代码片段如下
NSLog(@"URL::%@",url.absoluteString);
UIInterfaceOrientation interface = [UIApplication sharedApplication].statusBarOrientation;
videoPlayer = [[MPMoviePlayerController alloc] init];
[videoPlayer setContentURL:url];
[videoPlayer setMovieSourceType:MPMovieSourceTypeStreaming];
[videoPlayer setControlStyle:MPMovieControlStyleNone];
[videoPlayer setScalingMode:MPMovieScalingModeNone];
[videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - (viewTopbar.frame.size.height+[self getBannerHeight:interface]))];
[self.view addSubview:self.videoPlayer.view];
[self.videoPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinishWithReson:)
name:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey
object:videoPlayer];
#pragma mark
#pragma mark - movie player delegate methods
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackError) {
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
-(void) moviePlayBackDidFinishWithReson:(NSNotification *)notification
{
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackError) {
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
在对很多事情进行研发之后,我没有从委托方法中找到任何解决方案,所以对于这个问题,我实现了一件事,我启动一个计时器并检查电影播放器的当前头部是否大于 0。这意味着视频已成功加载和播放。
我的代码片段如下。
在 MPMoviePlayerController 的初始化中写入此代码
if (iOS6 && APPDELEGATE.isNetAvailable)
{
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkVideo) userInfo:nil repeats:NO];
}
并在您的视图控制器中编写此方法
-(void)checkVideo
{
NSLog(@"%f",videoPlayer.currentPlaybackTime);
if (videoPlayer.currentPlaybackTime == 0.0)
{
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[videoPlayer stop];
}
}
请仅将此解决方案用于 ios6。
我已经构建了一个演示,其中我只是播放实时视频 url,如果 url 无法正常工作,则 MPMoviePlayerController 会触发通知 MPMoviePlayerPlaybackDidFinishNotification MPMoviePlayerPlaybackDidFinishReasonUserInfoKey。这个东西在 ios7 和更高版本中完美运行,但是当我在 ios6 中使用同样的东西时,它不起作用。 我的代码片段如下
NSLog(@"URL::%@",url.absoluteString);
UIInterfaceOrientation interface = [UIApplication sharedApplication].statusBarOrientation;
videoPlayer = [[MPMoviePlayerController alloc] init];
[videoPlayer setContentURL:url];
[videoPlayer setMovieSourceType:MPMovieSourceTypeStreaming];
[videoPlayer setControlStyle:MPMovieControlStyleNone];
[videoPlayer setScalingMode:MPMovieScalingModeNone];
[videoPlayer.view setFrame:CGRectMake(0.0, viewTopbar.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - (viewTopbar.frame.size.height+[self getBannerHeight:interface]))];
[self.view addSubview:self.videoPlayer.view];
[self.videoPlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoPlayer];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinishWithReson:)
name:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey
object:videoPlayer];
#pragma mark
#pragma mark - movie player delegate methods
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackError) {
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
-(void) moviePlayBackDidFinishWithReson:(NSNotification *)notification
{
int reason = [[[notification userInfo] valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if (reason == MPMovieFinishReasonPlaybackError) {
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
在对很多事情进行研发之后,我没有从委托方法中找到任何解决方案,所以对于这个问题,我实现了一件事,我启动一个计时器并检查电影播放器的当前头部是否大于 0。这意味着视频已成功加载和播放。
我的代码片段如下。
在 MPMoviePlayerController 的初始化中写入此代码
if (iOS6 && APPDELEGATE.isNetAvailable)
{
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(checkVideo) userInfo:nil repeats:NO];
}
并在您的视图控制器中编写此方法
-(void)checkVideo
{
NSLog(@"%f",videoPlayer.currentPlaybackTime);
if (videoPlayer.currentPlaybackTime == 0.0)
{
//error
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:localize(@"strTitle") message:localize(@"strMsg") delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[videoPlayer stop];
}
}
请仅将此解决方案用于 ios6。