通过MPMoviePlayer使用凭据流式传输大型电影



我一直在尝试从受保护的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];           

那也没用。

从方法-(void)moviePlayBackDidFinish:(NSNotification*)通知我得到错误错误域=MediaPlayerDomain代码=-1013"操作无法完成。(MediaPlayerErrorDomain错误-1013。)">

查看苹果的文档,它是一个CFNetwork错误kCFURLErrorUserAuthenticationRequired=-1013

有什么办法解决这个问题吗?

我无法让MPMoviePlayerController正确地进行身份验证挑战,甚至认为苹果文档另有说法。我想出的一个非常巧妙的解决方案是使用苹果的CustomHTTPProtocol来拦截响应并提供身份验证质询响应。我相信这个协议最初的目的是处理UIWebViews的身份验证。

链接到CustomHTTPProtocol:https://developer.apple.com/library/ios/samplecode/CustomHTTPProtocol/Listings/Read_Me_About_CustomHTTPProtocol_txt.html

我的接口声明:

@interface SampleViewController() <CustomHTTPProtocolDelegate>

MPMoviePlayerController在我的SampleViewController:中的实例化

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一起工作,但它基本上是相同的代码。

希望这对你有用。

如果您的视频服务器需要基本身份验证,它可以很容易地传递到电影播放器控制器的URL,例如,您将以以下格式传递URL,而不是常规URL:

http(s)://user:password@host/path

然后将播放视频。

最新更新