2011-06-29 146 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"); 
}