ios - 通过 MPMoviePlayer 流式传输带有凭据的大型电影

标签 ios stream xcode5 mpmovieplayercontroller nsurlcredential

我一直在尝试从 protected 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;
}

我已经尝试将领域链接到 nil 没有工作。我尝试在之后移动 initWitcontnetURL
   [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential: credential forProtectionSpace: protectionSpace];           

那也没有用。

从方法 -(void) moviePlayBackDidFinish:(NSNotification*)notification
我收到错误 Error Domain=MediaPlayerDomain Code=-1013 “操作无法完成。(MediaPlayerErrorDomain 错误 -1013。)”

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

任何想法如何解决这个问题?

最佳答案

我无法获得 MPMoviePlayerController正确地进行身份验证挑战,即使 Apple 文档另有说明。我想出的非常 hacky 的解决方案是使用 Apple 的 CustomHTTPProtocol拦截响应并提供身份验证质询响应。我相信这个协议(protocol)的最初目的是处理 UIWebViews 的身份验证。 .

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

我的接口(interface)声明:

@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 ,我有几个委托(delegate)方法。对于基本身份验证,它非常简单:
- (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因为Apple提供了源代码,而且它真的很长。我进行了一些更改以使其与 ARC 一起使用,但它几乎是相同的代码。

希望这对你有用。

关于ios - 通过 MPMoviePlayer 流式传输带有凭据的大型电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22162404/

相关文章:

php - 如何将 stdin 的内容(直到 EOF)读入 PHP 中的字符串?

ios - Xcode 机器人 : cloning multiple repositories before build

ios - 带有应用程序图标效果的启动画面 IOS7

ios - 打开 Swift Playground 书

ios - 使用 WKHTTPCookieStore 删除 cookie

ios - ASIHttpRequest 调用 Restful Web 服务?

iphone - self.frame 与 self.layer.bounds(在 iOS 中)

python - 我如何使用 Python gRPC 处理流消息

.net - 流多播 - 一次读取一个流,但以不同的方式处理它,缓冲最少

localization - Xcode 5 和 .xib 文件的本地化