我正在尝试在OSX / iOS上执行AVAssetResourceLoaderDelegate的简单实现,作为一种健全性检查,然后再继续实现更复杂的版本。我想我会开始只是让它在一个大的读取中加载完整的视频文件。令我惊讶的是,我无法让它发挥作用。
这是我的代码:
- (BOOL) resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(nonnull AVAssetResourceLoadingRequest *)loadingRequest
{
NSString* path = @"/Users/andrew/Projects/work/videotextureplugin-streaming/unityProject/Assets/StreamingAssets/BigBuckBunny_640x360.m4v";
AVAssetResourceLoadingDataRequest* dataRequest = loadingRequest.dataRequest;
AVAssetResourceLoadingContentInformationRequest* contentRequest = loadingRequest.contentInformationRequest;
// handle content request
if (contentRequest)
{
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:NULL];
unsigned long long fileSize = [attributes fileSize]; // in bytes
NSLog(@"File Size: %llu", fileSize);
contentRequest.contentType = @"com.apple.m4v-video";
contentRequest.contentLength = fileSize;
contentRequest.byteRangeAccessSupported = NO;
}
// handle data request
if (dataRequest)
{
NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path];
NSData* requestedData = [file readDataToEndOfFile];
NSLog(@"Requested data length: %lu", (unsigned long)[requestedData length]);
[loadingRequest.dataRequest respondWithData:requestedData];
[loadingRequest finishLoading];
}
return YES;
}
打印时文件大小和数据长度值是正确的。它只是在日志中说“AVPlayer准备播放但是没有视频轨道”。我已经尝试了几个不同的视频文件,以及各种contentType设置,没有运气。如果我通过像“file:///”之类的URL打开它们,然后使用上面使用的相同路径,我测试的所有文件都可以正常工作。
提前感谢您提供有关调试的任何帮助或建议。