2

我一直在尝试从受保护的 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 "The operation could not be completed. (MediaPlayerErrorDomain error -1013.)"

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

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

4

2 回答 2

0

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

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

我的接口声明:

@interface SampleViewController() <CustomHTTPProtocolDelegate>

MPMoviePlayerController在 my 内实例化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,因为 Apple 提供了源代码,而且它真的很长。我进行了一些更改以使其与 ARC 一起使用,但它几乎是相同的代码。

希望这对你有用。

于 2014-04-05T07:18:36.830 回答
0

如果您的视频服务器需要基本身份验证,则可以轻松地将其传递给电影播放器​​控制器的 URL,例如,您将传递以下格式的 url,而不是常规 url:

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

然后将播放视频。

于 2015-11-03T20:14:49.810 回答