I'm downloading and caching (using https://github.com/vdugnist/DVAssetLoaderDelegate) videos in my app. When the app starts, I check for a downloaded file, and if it exists, I initialize the asset with the local file, otherwise, I initialize it with the remote URL. Then, when the download completes, I write the video file in the cache directory. I've checked the written file and it exists, and is a valid file (I've navigated the simulator's file system and found the file. It's there.).
When I launch the app the next time, I check for the existing file (it's successfully found), I initialize the player with that, but it never loads. I'm not getting any errors, but the buffer never gets any time ranges, video doesn't play, nothing is called etc.
Here is my code:
asset = [self resolvedAssetWithURL:url];
playerItem = [AVPlayerItem playerItemWithAsset:asset];
player = [AVPlayer playerWithPlayerItem:playerItem];
Where:
-(AVAsset*)resolvedAssetWithURL:(NSURL*)url{
NSURL *localURL = [self localURLForCachedRemoteURL:url];
if(localURL){
#if DEBUG
NSLog(@"Using cached copy for video at %@", url.absoluteString);
#endif
videoIsLocal = YES;
return [AVURLAsset URLAssetWithURL:localURL options:nil];
}else{
return [self assetWithoutCacheForURL:url];
}
}
Where:
-(NSURL*)localURLForCachedRemoteURL:(NSURL*)remoteURL{
NSString *cachedPath = [Cache cachedVideoDataRelativePathForURLPath:remoteURL.absoluteString];
if(cachedPath){
if([TUStorage fileExistsAtRelativePath:cachedPath]){
return [NSURL fileURLWithPath:[TUStorage absolutePathForRelativePath:cachedPath]];
}else{
NSLog(@"Remote URL %@ has been cached at %@ but local copy was not found. Removing cache.", remoteURL.absoluteString, cachedPath);
[Cache removeCachedVideoDataForURLPath:remoteURL.absoluteString];
return nil;
}
}else{
return nil;
}
}
I've got some custom methods there but you get the idea. The file is there and playable (it's the same data with the actual playing video when I load remotely with no issues).
I've took care of:
fileURLWithPath:
instead of the regular URL initializer when loading the local asset, as seen in the code.What am I doing wrong? (I am on iOS 11.2.6)
After hours of investigation, I've stumbled upon this question:
Is it possible to make AVURLAsset work without a file extension?
The question itself solved my problem: I wasn't using extensions for filenames. I've appended .mov
to the filename and it started working.