如何使用自定义NSURLProtocol使用URL播放电影?

时间:2010-12-08 01:27:52

标签: ios url mpmovieplayercontroller nsurlprotocol

如您所知,使用

使用MPMoviePlayerController对象播放电影
[[MPMoviePlayerController alloc] initWithContentURL: aURL];

现在,我想实现一个自定义的NSURLProtocol,我将在其中解密已被AlgorithmDES加密的电影源。 那可能吗?感谢您提供任何想法。需要帮助〜

4 个答案:

答案 0 :(得分:13)

更新:我与Apple谈到了这一点,目前无法将MPMoviePlayerController与NSURLProtocol子类一起使用!


HeJ小鼠,

我不确定但是有可能。我目前正在研究类似的东西,但还没有完全发挥作用。我发现MPMoviePlayerController与我的自定义NSURLProtocol子类进行交互但是考虑到NSURLRequest的HTTPHeaders似乎很重要,因为它们定义了MPMoviePlayerController需要的字节范围。

如果你将它们转储到你的NSURLProtocol子类中,你会得到两次这样的东西:

2011-01-16 17:00:47.287 iPhoneApp[1177:5f03] Start loading from request: {
Range = "bytes=0-1";

}

所以我的GUESS就是只要你能提供正确的范围并返回一个可以由MPMoviePlayerController播放的mp4文件就可以了!

编辑:这是一个有趣的链接:Protecting resources in iPhone and iPad apps

答案 1 :(得分:6)

解决方案是通过本地HTTP服务器代理请求。我使用CocoaHTTPServer完成了这项工作。

查看HTTPAsyncFileResponse示例。

答案 2 :(得分:5)

从iOS 7开始还有一个解决方案。您可以为AVAssetResourceLoader使用AVAssetResourceLoaderDelegate。但这只适用于AVPlayer。

Apple有一个名为AVARLDelegateDemo的演示项目,看看它,你应该找到你需要的东西。 (我认为链接到它并不是一个好主意,所以只需在developer.apple.com上的Developer Library中搜索它)然后使用任何自定义URL方案(不声明NSURLProtocol)并在AVAssetResourceLoaderDelegate中处理该URL方案

如果有巨大的兴趣,我可以提供概念验证要点。

答案 3 :(得分:1)

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

请注意使用虚拟网址强制AVPlayer使用AVAssetResourceLoaderDelegate方法,而不是直接访问网址。

相关问题