MPMoviePlayerViewController在几秒钟后停止

时间:2011-06-29 09:57:13

标签: ios objective-c video media-player mpmovieplayercontroller

我有一个应用程序,我在其中一个选项卡中流式传输直播电视频道。我正在使用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];
    }
}

然而,视频在两秒钟后冻结,应用程序停止响应。

2 个答案:

答案 0 :(得分:2)

您应该使用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");
}

答案 1 :(得分:0)

因为我的知识涉及计时器的使用,所以它很冷,并且需要时间流。

相关问题