簡體   English   中英

MPMoviePlayerViewController 幾秒鍾后停止

[英]MPMoviePlayerViewController stops after a few seconds

我有一個應用程序,我在其中一個選項卡中流式傳輸直播電視頻道。 我正在使用MPMoviePlayerViewController 我確實在我的 header 文件中聲明了我的MPMoviePlayerViewController並在我的實現文件中合成它。

這是我的 viewDidAppear:

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

還有我的 checkIt function

- (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 而不是手動輪詢當前的 state。

例如 - 在你初始化代碼的某個地方:

[[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 是 bitmap -> 您需要屏蔽要檢查的值。

例如:

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

據我所知,計時器的使用很凍結,而且流式傳輸也需要時間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM