ios - MediaPlayer.framework (MPMoviePlayerController) 从需要访问凭据的 URL 播放电影

标签 ios cocoa-touch mpmovieplayercontroller nsurlcredential

我在从具有基本 http 身份验证的 URL 播放电影时遇到问题。

代码如下:

NSURLCredential *credential = [[NSURLCredential alloc]
                               initWithUser:@"user"
                               password:@"password"
                               persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                             initWithHost:@"moze.dyndns.org"
                                             port:80
                                             protocol:@"http"
                                             realm:nil
                                             authenticationMethod:NSURLAuthenticationMethodHTTPBasic];

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];


NSURL *videoURL = [NSURL URLWithString:@"http://moze.dyndns.org/test/test.mov"];
moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[moviePlayerController.view setFrame:self.view.bounds];
[self.view addSubview:moviePlayerController.view];
MPMoviePlayerController *mp = [moviePlayerController moviePlayer];
mp.controlStyle = MPMovieControlStyleDefault;
[mp prepareToPlay];
[[moviePlayerController moviePlayer] play];

我收到错误“操作无法完成。(MediaPlayerErrorDomain 错误 -1013。)”,errorLog 为 NULL,就像 accessLog 一样。

我正在使用带有 AuthType Basic 的 apache 服务器,凭据是正确的,在网络浏览器上测试了它们。如果禁用身份验证,播放没有问题。

请帮忙,我找不到问题所在。

最佳答案

我无法让 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>

在我的 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:@"your-realm"
                                        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 - MediaPlayer.framework (MPMoviePlayerController) 从需要访问凭据的 URL 播放电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11289932/

相关文章:

ios - 从我的字典中获取图像导致 "unrecognized selector"错误

iOS 辅助功能 : Custom voice over text for bundle display name

ios - NSMakeRange(i, 1) 是什么意思?

iphone - 在观看时将 HTTP 直播视频录制到文件?

objective-c - MPMoviePlayerController 生成本地视频文件的缩略图并存储

objective-c - 以编程方式创建 UIView 和 UIViewController 并链接在一起的正确方法

ios - .AllowUserInteraction 不适用于 UIImageView

iphone - 使用 UILocalNotification 播放应用程序包中没有的声音

iphone - 如何播放文件名模糊的文件?

iphone - iPhone 上的下一个/上一个按钮 MPMoviePlayerController