How to play movie with a URL using a custom NSURLProtocol?

前端 未结 4 509
日久生厌
日久生厌 2020-12-08 05:48

As you know,play a movie with MPMoviePlayerController object using

[[MPMoviePlayerController alloc] initWithContentURL: aURL];

now ,i want

4条回答
  •  佛祖请我去吃肉
    2020-12-08 06:24

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

    Notice the use of a dummy URL to force AVPlayer to use the AVAssetResourceLoaderDelegate methods instead of accessing the URL directly.

提交回复
热议问题