[英]How to check live streaming url response 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没有响应。
谢谢,
使用以下通知
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
}
我对MPMoviePlayerControler
经验是错误处理不是很好。 一种可能是先请求URL,然后仅在流运行时加载.m3u8文件。
您可以使用NSURLConnection
和朋友来尝试加载流,并在必要时读取文件,以查找指示流正在运行的特定内容(也许存在包含#EXTINF:
的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.