如何检查实时流媒体网址响应iphone

时间:2012-07-13 12:15:15

标签: iphone mpmovieplayercontroller

在我的应用程序中,我正在使用以下代码进行实时流式传输:

-(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不响应。

谢谢,

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

我对MPMoviePlayerControler的体验是错误处理不是很好。一种可能性是首先请求URL,然后仅在流运行时加载.m3u8文件。

您可以使用NSURLConnection和朋友尝试加载流,并在必要时读取文件,查找指示流正在运行的特定内容(可能存在包含{{1}的行}。