2014-03-04 42 views
2

我一直在嘗試從受保護的URL流式傳輸電影。我可以下載電影然後播放,但電影太長,所以這很煩人。通過MPMoviePlayer流式傳輸大型電影憑證

這裏是我的代碼:

-(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; 
} 

我已經試過鏈接境界爲零,沒有工作。我試圖移動initWitcontnetURL

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

也沒有工作。

從方法 - (無效)moviePlayBackDidFinish:(NSNotification *)通知 我得到的錯誤錯誤域= MediaPlayerDomain代碼= -1013 「的操作無法完成(MediaPlayerErrorDomain錯誤-1013)。」

看着蘋果的文檔,這是一個CFNetwork錯誤kCFURLErrorUserAuthenticationRequired = -1013

任何想法如何解決這個問題?

回答

0

我無法得到MPMoviePlayerController正確地執行身份驗證挑戰,甚至認爲蘋果文檔中有其他說法。我提出的非常冒險的解決方案是使用Apple的CustomHTTPProtocol截獲響應並提供身份驗證質詢響應。我相信這個協議的最初目的是爲了處理UIWebViews的認證。

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

我的接口聲明:

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,我有一對夫婦的委託方法:MPMoviePlayerControllerSampleViewController

@interface SampleViewController() <CustomHTTPProtocolDelegate> 

實例化。對於基本身份驗證,這是很簡單的:

- (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一起工作,但它大部分是相同的代碼。

希望這對你有用。

+0

這看起來會有幫助。我對如何訂購所有東西感到困惑。可以評論一個鏈接到你的代碼,所以我可以看到你是如何做到的。 – kcajmagic

+0

我無法鏈接到我的代碼,但我使用我的相關代碼編輯了我的帖子 –

+0

這是否適合您?如果是這樣,你能把它標記爲可接受的解決方案嗎? –

0

如果您的視頻服務器需要基本身份驗證,它可以很容易地傳遞到電影播放器​​控制器的URL,例如,而不是普通的網址,你會通過URL格式:

http(s)://user:[email protected]/path 

然後視頻將播放。