2012-07-13 67 views
0

在我的應用我做現場直播與下面的代碼:如何檢查現場直播網址響應iphone

-(void)playurl:(id)sender 
{ 
    self.mpPlayer=[[[MPMoviePlayerController alloc]initWithContentURL:[NSURL URLWithString:@"http://alwatan.lsops.net/live/alwradio_ar_audio.sdp/playlist.m3u8"]] autorelease]; 

    [self.mpPlayer prepareToPlay]; 

    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     self.mpPlayer.view.frame = CGRectMake(0, 50, 768, 400);   
    } 
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) 
    { 
     self.mpPlayer.view.frame = CGRectMake(0, 0, 1024, 500); 
    }  
    [self.mpPlayer play]; 
    self.mpPlayer.movieSourceType = MPMovieSourceTypeStreaming; 
    //self.mpPlayer.movieSourceType = MPMovieSourceTypeFile; 
    self.mpPlayer.shouldAutoplay = TRUE; 


    self.mpPlayer.controlStyle = MPMovieControlStyleDefault; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:mpPlayer]; 
    [scrollView addSubview:self.mpPlayer.view]; 
    if(isVideoPlay) 
    { 
     isVideoPlay = FALSE; 
    } 
    else 
    { 
     isVideoPlay = TRUE; 
    } 
} 

- (void) moviePlayBackDidFinish:(NSNotification*)notification 
{ 
    NSError *error = [[notification userInfo] objectForKey:@"error"]; 
    if (error) { 
    } 
    else 
    { 
     MPMoviePlayerController *moviePlayer = [notification object]; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 
    } 
} 

一切都很好,但我的問題是URL沒有乳寧然後應用程序會因此崩潰我怎樣才能檢查實時流url響應,所以我可以管理的條件,並給予警報的url沒有迴應。

感謝,

回答

0

使用下列通知

MPMoviePlayerPlaybackStateDidChangeNotification 

和檢查

  if(moviePlayer.state == MPMoviePlaybackStateStopped) 
{ 
// Alert 
} 
else  if(moviePlayer.state == MPMoviePlaybackStatePlaying) 
{ 
// Alert 
} 
else  if(moviePlayer.state == MPMoviePlaybackStatePaused) 
{ 
// Alert 
} 
else  if(moviePlayer.state == MPMoviePlaybackStateInterrupted) 
{ 
// Alert 
} 

else  if(moviePlayer.state == MPMoviePlaybackStateSeekingForward) 
{ 
// Alert 
} 
else  if(moviePlayer.state == MPMoviePlaybackStateSeekingBackward) 
{ 
// Alert 
} 
+0

有你試過嗎? – 2012-07-13 12:29:44

+0

是的,但是當發生錯誤時,我給了worg ulr,然後它也會在其他情況下進入MPMoviePlaybackStateInterrupted條件 – Abhishek 2012-07-13 12:45:04

+0

然後檢查上面編輯的任何條件。 – 2012-07-13 12:52:52

0

我與MPMoviePlayerControler的經驗是,這不是很好的與錯誤處理。一種可能性是首先請求URL,然後只在流運行時加載.m3u8文件。

你可以使用NSURLConnection和朋友,試圖加載流,並且,如果需要的話,讀取文件尋找一些具體的指示流運行(也許含有#EXTINF:線的存在。