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

时间:2014-03-04 03:37:59

标签: ios stream xcode5 mpmovieplayercontroller nsurlcredential

我一直在尝试从受保护的网址流式传输电影。我可以下载电影然后播放,但电影太长,所以这很烦人。

这是我的代码:

-(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 *)通知 我收到错误Error Domain = MediaPlayerDomain Code = -1013“操作无法完成。(MediaPlayerErrorDomain error -1013。)”

查看Apple文档,它是CFNetwork错误kCFURLErrorUserAuthenticationRequired = -1013

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

2 个答案:

答案 0 :(得分:0)

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

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

我的界面声明:

@interface SampleViewController() <CustomHTTPProtocolDelegate>

MPMoviePlayerControllerSampleViewController的实例化:

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一起使用,但它的代码大致相同。

希望这适合你。

答案 1 :(得分:0)

如果您的视频服务器需要基本身份验证,则可以轻松将其传递到影片播放器控制器的网址,例如而不是常规网址,您将以格式传递网址:

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

然后播放视频。