由于与我的问题无关的原因,我必须使用自定义AVAssetResourceLoader实现AVPlayer播放AVURLAssets.一般来说,我的实现效果很好,但是当通过AirPlay尝试外部播放时(不镜像):
self.player.allowsExternalPlayback = YES;
self.player.usesExternalPlaybackWhileExternalScreenIsActive = YES;
播放失败,AppleTV上显示错误消息,并通过AVPlayerItemFailedToPlayToEndTimeNotification发布错误消息.
我能够通过简单的AVAssetResourceLoader实现重现相同的问题,该实现只是简单地分发本地媒体文件(包含在App Bundle中)的请求数据.这是我的实现:
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest; {
AVAssetResourceLoadingDataRequest *dataRequest = loadingRequest.dataRequest;
AVAssetResourceLoadingContentInformationRequest *contentRequest = loadingRequest.contentInformationRequest;
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"localfile" withExtension:@"mov"];
if (contentRequest)
{
NSString *extension = [fileURL pathExtension];
NSString *contentTypeString = (NSString*)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)extension,NULL);
contentRequest.contentType = contentTypeString;
contentRequest.contentLength = [[[NSFileManager defaultManager] attributesOfItemAtPath:[fileURL path] error:nil] fileSize];
contentRequest.byteRangeAccessSupported = YES;
}
if (dataRequest)
{
NSInteger length = [dataRequest requestedLength];
if (length>1024*1024*20)
{
// limiting to 20MB here because iOS sometimes requests the whole file which would not fit in memory
length = 1024*1024*20;
}
NSFileHandle *fh = [NSFileHandle fileHandleForReadingFromURL:fileURL error:nil];
[fh seekToFileOffset:[dataRequest requestedOffset]];
NSData *fileData = [fh readDataOfLength:length];
[dataRequest respondWithData:fileData];
[fh closeFile];
}
[loadingRequest finishLoading];
return YES;}
此代码直接在设备上播放.如果启用了AirPlay,则会调用资源加载器,内容和数据请求大约4-5次,请求项目的前8kb.然后回调停止,最终我在Apple TV和一些AVPlayerItemFailedToPlayToEndTimeNotifications上收到错误消息.第一个通知包含“不支持的媒体”错误.
(通过将其添加到AVPlayer中播放相同的媒体作为基于正常文件的项目也可通过AirPlay播放).
任何输入都将非常感谢,谢谢!
最佳答案 我现在收到Apple(通过TSI)对此问题的回复.
使用自定义资源加载程序时不支持Video AirPlay.