fix: 🐛 support ios cameraroll

This commit is contained in:
virgil 2022-04-15 17:29:56 +08:00
parent 63f12eaed6
commit 285e9ca881
2 changed files with 42 additions and 24 deletions

View File

@ -275,7 +275,7 @@ export default class Video extends Component {
} }
const isNetwork = !!(uri && uri.match(/^https?:/)); const isNetwork = !!(uri && uri.match(/^https?:/));
const isAsset = !!(uri && uri.match(/^(assets-library|ipod-library|file|content|ms-appx|ms-appdata):/)); const isAsset = !!(uri && uri.match(/^(assets-library|ph|ipod-library|file|content|ms-appx|ms-appdata):/));
let nativeResizeMode; let nativeResizeMode;
const RCTVideoInstance = this.getViewManagerConfig('RCTVideo'); const RCTVideoInstance = this.getViewManagerConfig('RCTVideo');

View File

@ -5,6 +5,7 @@
#import <React/UIView+React.h> #import <React/UIView+React.h>
#include <MediaAccessibility/MediaAccessibility.h> #include <MediaAccessibility/MediaAccessibility.h>
#include <AVFoundation/AVFoundation.h> #include <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
static NSString *const statusKeyPath = @"status"; static NSString *const statusKeyPath = @"status";
static NSString *const playbackLikelyToKeepUpKeyPath = @"playbackLikelyToKeepUp"; static NSString *const playbackLikelyToKeepUpKeyPath = @"playbackLikelyToKeepUp";
@ -492,22 +493,22 @@ static int const RCTVideoUnset = -1;
handler([AVPlayerItem playerItemWithAsset:mixComposition]); handler([AVPlayerItem playerItemWithAsset:mixComposition]);
} }
- (void)playerItemForSource:(NSDictionary *)source withCallback:(void(^)(AVPlayerItem *))handler - (void)loadAssetFromSource:(NSDictionary *)source withCallback:(void(^)(AVURLAsset *asset, NSMutableDictionary *assetOptions)) handler {
{
bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]]; bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]];
bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]]; bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]];
bool shouldCache = [RCTConvert BOOL:[source objectForKey:@"shouldCache"]]; bool shouldCache = [RCTConvert BOOL:[source objectForKey:@"shouldCache"]];
NSString *uri = [source objectForKey:@"uri"]; NSString *uri = [source objectForKey:@"uri"];
NSString *type = [source objectForKey:@"type"]; NSString *type = [source objectForKey:@"type"];
AVURLAsset *asset;
if (!uri || [uri isEqualToString:@""]) { if (!uri || [uri isEqualToString:@""]) {
DebugLog(@"Could not find video URL in source '%@'", source); DebugLog(@"Could not find video URL in source '%@'", source);
return; return;
} }
NSURL *url = isNetwork || isAsset NSURL *url = isNetwork || isAsset
? [NSURL URLWithString:uri] ? [NSURL URLWithString:uri]
: [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]]; : [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc] init]; NSMutableDictionary *assetOptions = [[NSMutableDictionary alloc] init];
if (isNetwork) { if (isNetwork) {
@ -529,26 +530,43 @@ static int const RCTVideoUnset = -1;
return; return;
} }
#endif #endif
handler([AVURLAsset URLAssetWithURL:url options:assetOptions], assetOptions);
asset = [AVURLAsset URLAssetWithURL:url options:assetOptions];
} else if (isAsset) { } else if (isAsset) {
asset = [AVURLAsset URLAssetWithURL:url options:nil]; if ([uri hasPrefix:@"ph://"]) {
NSString *assetId = [uri substringFromIndex:@"ph://".length];
PHAsset *asset = [[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil] firstObject];
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.networkAccessAllowed = YES;
[[PHCachingImageManager new] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
handler((AVURLAsset *)asset, assetOptions);
}];
} else {
handler([AVURLAsset URLAssetWithURL:url options:nil], assetOptions);
}
} else { } else {
asset = [AVURLAsset URLAssetWithURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]] options:nil]; AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]] options:nil];
handler(asset, assetOptions);
} }
// Reset _loadingRequest }
if (_loadingRequest != nil) {
[_loadingRequest finishLoading]; - (void)playerItemForSource:(NSDictionary *)source withCallback:(void(^)(AVPlayerItem *))handler
} {
_requestingCertificate = NO; [self loadAssetFromSource:source withCallback:^(AVURLAsset *asset, NSMutableDictionary *assetOptions) {
_requestingCertificateErrored = NO; // Reset _loadingRequest
// End Reset _loadingRequest if (self->_loadingRequest != nil) {
if (self->_drm != nil) { [self->_loadingRequest finishLoading];
dispatch_queue_t queue = dispatch_queue_create("assetQueue", nil); }
[asset.resourceLoader setDelegate:self queue:queue]; self->_requestingCertificate = NO;
} self->_requestingCertificateErrored = NO;
// End Reset _loadingRequest
[self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler]; if (self->_drm != nil) {
dispatch_queue_t queue = dispatch_queue_create("assetQueue", nil);
[asset.resourceLoader setDelegate:self queue:queue];
}
[self playerItemPrepareText:asset assetOptions:assetOptions withCallback:handler];
}];
} }
#if __has_include(<react-native-video/RCTVideoCache.h>) #if __has_include(<react-native-video/RCTVideoCache.h>)
@ -2028,4 +2046,4 @@ didCancelLoadingRequest:(AVAssetResourceLoadingRequest *)loadingRequest {
} }
#endif #endif
@end @end