我一直在尝试从一个受保护的url流式播放一部电影。我可以下载电影然后播放,但是电影太长了,所以这很烦人。
下面是我的代码:
-(MPMoviePlayerController *)moviePlayerController
{
NSURL *url = [NSURL URLWithString:@"http://ABcDE.com/secret/Movie.mov"];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser: @"user"
password: @"password"
persistence: NSURLCredentialPersistencePermanent];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: [url host]
port: 80
protocol: [url scheme]
realm: [url host]
authenticationMethod: NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage]
setDefaultCredential: credential
forProtectionSpace: protectionSpace];
_moviePlayer.view.frame = CGRectMake(0, 0, 500, 500);
_moviePlayer.controlStyle = MPMovieControlStyleDefault;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
_moviePlayer.allowsAirPlay = YES;
_moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
return _moviePlayer;
}我试着把这个领域链接到零,但没有成功。之后我尝试移动initWitcontnetURL
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential: credential forProtectionSpace: protectionSpace]; 这也不管用。
从方法-(空)代码中,我得到了错误代码Domain=MediaPlayerDomain =-1013“无法完成操作。(MediaPlayerErrorDomain错误-1013。)”
查看apple文档,这是一个CFNetwork错误kCFURLErrorUserAuthenticationRequired = -1013
有什么办法解决这个问题吗?
发布于 2014-04-05 15:18:37
我无法让MPMoviePlayerController正确地完成身份验证挑战,即使苹果公司的文档中有不同的说法。我想出了一个非常老套的解决方案,就是使用苹果的CustomHTTPProtocol拦截响应,并提供身份验证质询响应。我相信这个协议的最初目的是处理UIWebViews的身份验证。
链接到CustomHTTPProtocol:https://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Listings/Read_Me_About_CustomHTTPProtocol_txt.html
我的接口声明:
@interface SampleViewController() <CustomHTTPProtocolDelegate>在我的SampleViewController中实例化MPMoviePlayerController
NSString *fullURLString = @"http://www.samplesite.com/samplemovie.mp4";
NSURL *fullURL = [NSURL URLWithString:fullURLString];
[CustomHTTPProtocol setDelegate:self];
[CustomHTTPProtocol start];
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser:@"username"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:fullURL.host
port:80
protocol:fullURL.scheme
realm:nil
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fullURL];
[self.moviePlayer prepareToPlay];
[self.moviePlayer setShouldAutoplay:NO];
[self.moviePlayer setControlStyle:MPMovieControlStyleEmbedded];
[self.moviePlayer.view setFrame:self.sampleView.bounds];
[self.moviePlayer.backgroundView setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1.0]];
[self.moviePlayer.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self.sampleView addSubview:self.moviePlayer.view];同样在我的SampleViewController中,我有几个委托方法。对于基本身份验证,它非常简单:
- (BOOL)customHTTPProtocol:(CustomHTTPProtocol *)protocol canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
BOOL canAuth = ([[protectionSpace authenticationMethod] isEqual:NSURLAuthenticationMethodHTTPBasic] &&
[[protectionSpace realm] isEqualToString:<your realm>]);
return canAuth;
}
- (void)customHTTPProtocol:(CustomHTTPProtocol *)protocol didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:<username>
password:<password>
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}在调用start之后,所有的http和https请求都会经过CustomHTTPProtocol模块
我没有包括CustomHTTPProtocol,因为苹果提供了源代码,而且它真的很长。我做了一些修改,使其与ARC工作,但它大部分是相同的代码。
希望这对你有用。
发布于 2015-11-04 04:14:50
如果您的视频服务器需要基本身份验证,则可以轻松地将其传递到电影播放器控制器的URL,例如,您将以以下格式传递url,而不是常规的url:
http(s)://user:password@host/path然后将播放视频。
https://stackoverflow.com/questions/22162404
复制相似问题