2011-06-29 33 views
0

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

不过两秒后的视频冻结和应用程序停止响应。

+0

什么checkIt方法呢? – Maulik

+0

你为什么使用[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(checkIt)userInfo:nil repeatats:NO];? – iMOBDEV

+0

checkit方法控制电影是否处于可播放状态。如果电影不处于可播放状态,则等待0.1秒(nstimer)并再次重新调用checkit方法。 –

回答

0

Asfar作为我的知识关注定时器的使用它冻结,它也需要时间流。

+0

我不认为这是计时器,因为我不会用定时器将玩家推到我的视野。计时器只是等待电影准备就绪。 –

2

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

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

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

现在执行的通知处理程序:

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

并在您的deinitializing代码的地方:

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

还要注意, MPMoviePlayerController.loadState是一个位图 - >你需要掩盖你的值nt来检查。

例如:

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