MPMoviePlayerViewController

MPMoviePlayerViewController

我有一个应用程序,可以在其中一个标签页中播放直播电视频道。我正在使用MPMoviePlayerViewController。我确实在头文件中声明了MPMoviePlayerViewController并在实现文件中对其进行了综合。

这是我的viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
    NSURL *movieURL = [[NSURL alloc]initWithString:@"http://mysuperURL"];
    moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self checkIt];
}


还有我的checkIt函数

- (void) checkIt {
    if ([[moviePlayerController moviePlayer] loadState] == MPMovieLoadStateUnknown) { // before you wreck yourself
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkIt) userInfo:nil repeats:NO];
    } else {
        [moviePlayerController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        [self presentModalViewController:moviePlayerController animated:YES];
    }
}


但是,视频在两秒钟后冻结,并且该应用程序停止响应。

最佳答案

您应该使用MPMoviePlayerNotifications而不是手动轮询当前状态。

例如-在您初始化代码的某处:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(MPMoviePlayerLoadStateDidChange:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:nil];


现在实现一个通知处理程序:

- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
    NSLog(@"loadstate change: %Xh", movieController_.loadState);
}


在您的反初始化代码中的某处:

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerLoadStateDidChangeNotification
                                                  object:nil];


还要注意,MPMoviePlayerController.loadState是一个位图->您需要屏蔽要检查的值。

例如:

if ((movieController_.loadState & MPMovieLoadStatePlayable) == MPMovieLoadStatePlayable)
{
    NSLog(@"yay, it became playable");
}

10-06 00:40