通过MPMoviePlayer传输具有凭据的大型电影
我一直在尝试从受保护的URLstream式传输电影。 我可以下载电影然后播放,但电影太长,所以这是烦人的。
这是我的代码:
-(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];
那也不pipe用。
从该方法 – (void)moviePlayBackDidFinish:(NSNotification *)通知我得到错误Error Domain = MediaPlayerDomain Code = -1013“操作无法完成。(MediaPlayerErrorDomain error -1013。)”
看着苹果文档,这是一个CFNetwork错误kCFURLErrorUserAuthenticationRequired = -1013
任何想法如何解决这个?
我无法让MPMoviePlayerController
正确地执行身份validation挑战,甚至认为苹果公司的文件说不然。 我提出的非常冒险的解决scheme是使用苹果的CustomHTTPProtocol
拦截响应并提供身份validation质询响应。 我相信这个协议的最初目的是处理UIWebViews
身份validation。
链接到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: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
,我有几个委托方法。 对于基本身份validation,这非常简单:
- (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工作,但它是大部分相同的代码。
希望这对你有用。
如果您的video服务器需要基本身份validation,则可以轻松地将其传递到电影播放器控制器的URL,例如,而不是常规URL,您将传递URL格式:
http(s)://user:password@host/path
然后video将被播放。
- Restkit请求加载处理程序
- 如何从ios sdk库中获取所有video文件的列表
- iOS – Google AdMob v6.12.0 – “idfa class missing,不会收集idfa”
- UIImageView放大和捏在UIScrollView
- PHP来编辑XML,从iOS发布
- 如何在IOS5中自定义UINavigationBar
- Xcode表格视图在滚动时滞后
- 如何将代码块分派到iOS中的同一线程?
- launchOptions’UIApplicationLaunchOptionsLocalNotificationKey’是否包含NSDictionary或UILocalNotification