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

4

我遇到了从带有基本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.)”,错误日志为NULL,访问日志也是如此。

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

请帮忙解决,我找不出问题所在。


1
完全有可能电影播放器不会查询URL凭据存储,因为它在内部没有使用URL加载系统 :( - Mike Abdullah
那么,有没有其他方法可以播放需要身份验证的 URL 媒体? - Moze
我很想知道你在这方面有什么想法。我同意Mike Abdullah的观点,不要使用你设置的凭据。我也遇到了同样的问题。一旦我成功设置了凭据,就无法删除它们。我找到的解决方案都没有帮助。 - SteveB
我最终采用了不同的方法——生成临时开放链接,只允许一定数量的连接。 - Moze
1个回答

0

我无法让MPMoviePlayerController正确处理身份验证挑战,即使苹果文档中说的是另外一种方式。我想出的非常hacky的解决方案是使用苹果的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:@"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中,我有几个委托方法。对于基本身份验证,它非常简单:
- (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 兼容,但大部分代码都是相同的。

希望这对你有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接