diff --git a/ios/VideoCaching/RCTVideoCachingHandler.swift b/ios/VideoCaching/RCTVideoCachingHandler.swift index 8ccc4aaae5..fa1f2eab16 100644 --- a/ios/VideoCaching/RCTVideoCachingHandler.swift +++ b/ios/VideoCaching/RCTVideoCachingHandler.swift @@ -5,7 +5,7 @@ import Promises class RCTVideoCachingHandler: NSObject, DVAssetLoaderDelegatesDelegate { private var _videoCache: RCTVideoCache! = RCTVideoCache.sharedInstance() - var playerItemPrepareText: ((AVAsset?, NSDictionary?, String) -> AVPlayerItem)? + var playerItemPrepareText: ((AVAsset?, NSDictionary?, String) -> Promise)? override init() { super.init() @@ -29,7 +29,7 @@ class RCTVideoCachingHandler: NSObject, DVAssetLoaderDelegatesDelegate { func playerItemForSourceUsingCache(uri: String!, assetOptions options: NSDictionary!) -> Promise { let url = URL(string: uri) return getItemForUri(uri) - .then { [weak self] (videoCacheStatus: RCTVideoCacheStatus, cachedAsset: AVAsset?) -> AVPlayerItem in + .then { [weak self] (videoCacheStatus: RCTVideoCacheStatus, cachedAsset: AVAsset?) -> Promise in guard let self = self, let playerItemPrepareText = self.playerItemPrepareText else { throw NSError(domain: "", code: 0, userInfo: nil) } switch videoCacheStatus { case .missingFileExtension: @@ -56,7 +56,9 @@ class RCTVideoCachingHandler: NSObject, DVAssetLoaderDelegatesDelegate { if let cachedAsset = cachedAsset { DebugLog("Playing back uri '\(uri)' from cache") // See note in playerItemForSource about not being able to support text tracks & caching - return AVPlayerItem(asset: cachedAsset) + return Promise { + AVPlayerItem(asset: cachedAsset) + } } } @@ -75,7 +77,12 @@ class RCTVideoCachingHandler: NSObject, DVAssetLoaderDelegatesDelegate { asset?.resourceLoader.setDelegate(resourceLoaderDelegate, queue: DispatchQueue.main) */ - return AVPlayerItem(asset: asset) + return Promise { + AVPlayerItem(asset: asset) + } + }.then { playerItem -> AVPlayerItem? in + guard let playerItem = playerItem else { throw NSError(domain: "", code: 0, userInfo: nil) } + return playerItem } }