refactor: 💡 support camera video for swift
This commit is contained in:
parent
f4827e12f4
commit
fcea0ffcea
@ -1,5 +1,6 @@
|
|||||||
import AVFoundation
|
import AVFoundation
|
||||||
import Promises
|
import Promises
|
||||||
|
import Photos
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Collection of pure functions
|
* Collection of pure functions
|
||||||
@ -264,8 +265,20 @@ enum RCTVideoUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static func preparePHAsset(uri: String) -> Promise<AVAsset?> {
|
||||||
|
return Promise<AVAsset?>(on: .global()) { fulfill, reject in
|
||||||
|
let assetId = String(uri[uri.index(uri.startIndex, offsetBy: "ph://".count)...])
|
||||||
|
let phAsset = PHAsset.fetchAssets(withLocalIdentifiers: [assetId], options: nil).firstObject
|
||||||
|
let options = PHVideoRequestOptions()
|
||||||
|
options.isNetworkAccessAllowed = true
|
||||||
|
PHCachingImageManager().requestAVAsset(forVideo: phAsset!, options: options) { data, _, _ in
|
||||||
|
fulfill(data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static func prepareAsset(source:VideoSource) -> (asset:AVURLAsset?, assetOptions:NSMutableDictionary?)? {
|
static func prepareAsset(source:VideoSource) -> (asset:AVURLAsset?, assetOptions:NSMutableDictionary?)? {
|
||||||
guard source.uri != nil && source.uri != "" else { return nil }
|
guard let sourceUri = source.uri, sourceUri != "" else { return nil }
|
||||||
var asset:AVURLAsset!
|
var asset:AVURLAsset!
|
||||||
let bundlePath = Bundle.main.path(forResource: source.uri, ofType: source.type) ?? ""
|
let bundlePath = Bundle.main.path(forResource: source.uri, ofType: source.type) ?? ""
|
||||||
let url = source.isNetwork || source.isAsset
|
let url = source.isNetwork || source.isAsset
|
||||||
|
@ -227,10 +227,20 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
|
|||||||
RCTVideoUtils.delay()
|
RCTVideoUtils.delay()
|
||||||
.then{ [weak self] in
|
.then{ [weak self] in
|
||||||
guard let self = self else {throw NSError(domain: "", code: 0, userInfo: nil)}
|
guard let self = self else {throw NSError(domain: "", code: 0, userInfo: nil)}
|
||||||
guard let source = self._source,
|
guard let source = self._source else {
|
||||||
let assetResult = RCTVideoUtils.prepareAsset(source: source),
|
DebugLog("The source not exist")
|
||||||
let asset = assetResult.asset,
|
throw NSError(domain: "", code: 0, userInfo: nil)
|
||||||
let assetOptions = assetResult.assetOptions else {
|
}
|
||||||
|
if let uri = source.uri, uri.starts(with: "ph://") {
|
||||||
|
return Promise {
|
||||||
|
RCTVideoUtils.preparePHAsset(uri: uri).then { asset in
|
||||||
|
return self.playerItemPrepareText(asset:asset, assetOptions:nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
guard let assetResult = RCTVideoUtils.prepareAsset(source: source),
|
||||||
|
let asset = assetResult.asset,
|
||||||
|
let assetOptions = assetResult.assetOptions else {
|
||||||
DebugLog("Could not find video URL in source '\(self._source)'")
|
DebugLog("Could not find video URL in source '\(self._source)'")
|
||||||
throw NSError(domain: "", code: 0, userInfo: nil)
|
throw NSError(domain: "", code: 0, userInfo: nil)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user