gpt4 book ai didi

ios - MPMoviePlayerController Bad Url 错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:42:08 26 4
gpt4 key购买 nike

如果我给 MPMoviePlayerViewController 一个错误的视频 URL 来播放,就像这样:

[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://badurl"]];

有没有办法通知视频未下载?

我尝试了以下两种方法,但都没有收到通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateChanged) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

最佳答案

首先使用 Rechability 检查 URL 是否可达。

 NSURL *myURL = [NSURL urlWithString: @"http://example.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: myURL];
[request setHTTPMethod: @"HEAD"];
dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_BACKGROUND, NULL), ^{
NSURLResponse *response;
NSError *error;
NSData *myData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];
BOOL reachable;

if (myData) {
// we are probably reachable, check the response
reachable=YES;
} else {
// we are probably not reachable, check the error:
reachable=NO;
}

// now call ourselves back on the main thread
dispatch_async( dispatch_get_main_queue(), ^{
[self setReachability: reachable];
});
});

它提到的是这个答案link

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];


- (void) playbackDidFinish:(NSNotification*)notification{
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
NSLog(@"Playback Ended");
break;
case MPMovieFinishReasonPlaybackError:
NSLog(@"Playback Error"); //// this include Bad URL
break;
case MPMovieFinishReasonUserExited:
NSLog(@"User Exited");
break;
default:
break;
}
}

或者添加一个自定义函数来检查请求超时..

[self performSelector:@selector(checkTimeout:) withObject:theMovie afterDelay:15];

如果您在代码中没有收到任何通知
请检查此答案以供进一步引用。 link

关于ios - MPMoviePlayerController Bad Url 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26184103/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com