如您所知,使用 MPMoviePlayerController 对象播放电影
[[MPMoviePlayerController alloc] initWithContentURL: aURL];
现在,我想实现一个自定义的 NSURLProtocol,我将在其中解密已由 AlgorithmDES 加密的电影源。有这种可能吗?感谢您提供任何想法。需要您的帮助~
如您所知,使用 MPMoviePlayerController 对象播放电影
[[MPMoviePlayerController alloc] initWithContentURL: aURL];
现在,我想实现一个自定义的 NSURLProtocol,我将在其中解密已由 AlgorithmDES 加密的电影源。有这种可能吗?感谢您提供任何想法。需要您的帮助~
更新:我与 Apple 谈过此事,目前无法将 MPMoviePlayerController 与 NSURLProtocol 子类一起使用!
嘿嘿,
我不确定,但有可能。我目前正在做类似的事情,但还没有完全发挥作用。我发现 MPMoviePlayerController 与我的自定义 NSURLProtocol 子类交互,但考虑到 NSURLRequest 的 HTTPHeaders 似乎很重要,因为它们定义了 MPMoviePlayerController 需要的字节范围。
如果你将它们转储到你的 NSURLProtocol 子类中,你会在开始时得到两次这样的东西:
2011-01-16 17:00:47.287 iPhoneApp[1177:5f03] Start loading from request: {
Range = "bytes=0-1";
}
所以我的猜测是,只要你能提供正确的范围并返回一个可以由 MPMoviePlayerController 播放的 mp4 文件,它应该是可能的!
编辑:这是一个有趣的链接:保护 iPhone 和 iPad 应用程序中的资源
解决方案是通过本地 HTTP 服务器代理请求。我已经使用CocoaHTTPServer完成了这项工作。
看HTTPAsyncFileResponse
例子。
从 iOS 7 开始,还有另一种解决方案。您可以将 AVAssetResourceLoaderDelegate 用于 AVAssetResourceLoader。但这仅适用于 AVPlayer 。
苹果有一个名为 AVARLDelegateDemo 的演示项目,看看它,你应该会找到你需要的。(我认为链接到它不是一个好主意,所以只需在 developer.apple.com 上的开发人员库中搜索它)然后使用任何自定义 URL 方案(不声明 NSURLProtocol)并在 AVAssetResourceLoaderDelegate 中处理该 URL 方案。
如果有很大的兴趣,我可以提供概念证明要点。
@property AVPlayerViewController *avPlayerVC;
@property NSData *yourDataSource
// initialise avPlayerVC
NSURL *dummyURL = [NSURL URLWithString:@"foobar://dummy.mov"];// a non-reachable URL will force the use of the resourceLoader
AVURLAsset *asset = [AVURLAsset assetWithURL:dummyURL];
[asset.resourceLoader setDelegate:self queue:dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0)];
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
self.avPlayerVC.player = [AVPlayer playerWithPlayerItem:item];
self.avPlayerVC.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
// implement AVAssetResourceLoaderDelegate
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest {
loadingRequest.contentInformationRequest.contentType = (__bridge NSString *)kUTTypeQuickTimeMovie;
loadingRequest.contentInformationRequest.contentLength = self.yourDataSource.length;
loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES;
NSRange range = NSMakeRange((NSUInteger)loadingRequest.dataRequest.requestedOffset, loadingRequest.dataRequest.requestedLength);
[loadingRequest.dataRequest respondWithData:[self.yourDataSource subdataWithRange:range]];
[loadingRequest finishLoading];
return YES;
}
请注意使用虚拟 URL 来强制AVPlayer
使用AVAssetResourceLoaderDelegate
方法而不是直接访问 URL。