Skip to content

Instantly share code, notes, and snippets.

@jfpoole
Created February 12, 2018 20:04
Show Gist options
  • Save jfpoole/23decd414c00a275bbbf40bae970ccfd to your computer and use it in GitHub Desktop.
Save jfpoole/23decd414c00a275bbbf40bae970ccfd to your computer and use it in GitHub Desktop.
// VoodooPad
//
// Copyright (C) 2004-2018 Primate Labs Inc. All Rights Reserved.
//////////////////////////////////////////////////////////////////////////////
#import "VPQTTabViewController.h"
NSString* const VPItemScheme = @"x-voodoopad-item";
@interface VPItemLoaderDelegate : NSObject <AVAssetResourceLoaderDelegate>
@property (nonatomic, strong) VPItem* item;
@end
@implementation VPItemLoaderDelegate
- (instancetype)initWithItem:(VPItem*)item
{
self = [super init];
if (self) {
self.item = item;
}
return self;
}
/*!
* is scheme supported
*/
- (BOOL) schemeSupported:(NSString *)scheme
{
if ([scheme isEqualToString:VPItemScheme]) {
return YES;
}
return NO;
}
- (void) reportError:(AVAssetResourceLoadingRequest *) loadingRequest withErrorCode:(int) error
{
NSLog(@"%d", error);
}
// https://github.com/leshkoapps/AVAssetResourceLoader/blob/6f633a5c6568ad42dad287280799eb139463d1e6/ResourceLoader/LSFilePlayerResourceLoader.m
- (BOOL) resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{
NSURL *resourceURL = [loadingRequest.request URL];
if ([resourceURL.scheme isEqualToString:VPItemScheme]) {
NSData* itemData = [self.item data];
long long offset = loadingRequest.dataRequest.requestedOffset;
long length = loadingRequest.dataRequest.requestedLength;
if (offset >= [itemData length]) {
offset = [itemData length] - 1;
}
if ((offset + length) >= [itemData length]) {
length = [itemData length] - offset;
}
const char* itemBuffer = (const char*) [itemData bytes];
NSData* requestData = [NSData dataWithBytes:(itemBuffer + offset) length:length];
if (loadingRequest.contentInformationRequest) {
NSString* uti = [[self item] metaValueForKey:VPUTIMetaName];
loadingRequest.contentInformationRequest.contentType = uti;
loadingRequest.contentInformationRequest.contentLength = [itemData length];
loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES;
}
[loadingRequest.dataRequest respondWithData:requestData];
[loadingRequest finishLoading];
return YES;
}
return NO;
}
@end
@implementation VPQTTabViewController
- (void)loadMov
{
NSString* scratch = [NSString stringWithFormat:@"%@://%@", VPItemScheme, [[self item] uuid]];
NSURL* url = [NSURL URLWithString:scratch];
AVURLAsset* asset = [AVURLAsset assetWithURL:url];
VPItemLoaderDelegate* delegate = [[VPItemLoaderDelegate alloc] initWithItem:[self item]];
AVAssetResourceLoader *resourceLoader = asset.resourceLoader;
[resourceLoader setDelegate:delegate queue:dispatch_queue_create("VPItemLoaderDelegate", nil)];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];
AVPlayer* player = [AVPlayer playerWithPlayerItem:playerItem];
movieView.player = player;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment