AVAssetResourceLoader backed AVURLAsset playback fails only via AirPlay

萝らか妹 提交于 2019-12-23 12:23:58

问题


For reasons irrelevant to my question I have to implement an AVPlayer playing back AVURLAssets using a custom AVAssetResourceLoader. In general my implementation works well, but when attempting external playback via AirPlay (not mirroring):

 self.player.allowsExternalPlayback = YES;
 self.player.usesExternalPlaybackWhileExternalScreenIsActive = YES;

The playback fails with an error message on the AppleTV and an error message posted via AVPlayerItemFailedToPlayToEndTimeNotification.

I am able to reproduce the same issue with a trivial AVAssetResourceLoader implementation which simply hands out the requested data of a local media file (included in the App Bundle). Here is my implementation:

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

This code plays back fine on the device directly. If AirPlay is enabled, the resourceloader is called with content and data requests about 4-5 times, requesting the first 8kb of the item. The callbacks then stop and eventually I get a error message on the Apple TV and a few AVPlayerItemFailedToPlayToEndTimeNotifications. The first notification contains a "media not supported" error.

(Playing the same piece of media by adding it to the AVPlayer as a normal file based item plays fine via AirPlay as well).

Any input would be much appreciated, thanks!


回答1:


I have now received a response from Apple (via a TSI) to this question. Video AirPlay is not supported when using a custom resource loader.



来源:https://stackoverflow.com/questions/30752659/avassetresourceloader-backed-avurlasset-playback-fails-only-via-airplay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!