diff --git a/ios/ReactNativeVideoPlayerView.h b/ios/ReactNativeVideoPlayerView.h index 88d9087..d274cc9 100644 --- a/ios/ReactNativeVideoPlayerView.h +++ b/ios/ReactNativeVideoPlayerView.h @@ -32,12 +32,12 @@ NS_ASSUME_NONNULL_END @interface ReactNativeVideoPlayerView : RCTView -@property (nonatomic, copy) RCTDirectEventBlock onReadyForDisplay; -@property (nonatomic, copy) RCTDirectEventBlock onLoad; -@property (nonatomic, copy) RCTDirectEventBlock onProgress; -@property (nonatomic, copy) RCTDirectEventBlock onEnd; -@property (nonatomic, copy) RCTDirectEventBlock onBuffer; -@property (nonatomic, copy) RCTDirectEventBlock onError; +@property(nonatomic, copy) RCTDirectEventBlock onReadyForDisplay; +@property(nonatomic, copy) RCTDirectEventBlock onLoad; +@property(nonatomic, copy) RCTDirectEventBlock onProgress; +@property(nonatomic, copy) RCTDirectEventBlock onEnd; +@property(nonatomic, copy) RCTDirectEventBlock onBuffer; +@property(nonatomic, copy) RCTDirectEventBlock onError; - (instancetype)initWithBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER; diff --git a/ios/ReactNativeVideoPlayerView.mm b/ios/ReactNativeVideoPlayerView.mm index ac4f2f2..bb02101 100644 --- a/ios/ReactNativeVideoPlayerView.mm +++ b/ios/ReactNativeVideoPlayerView.mm @@ -1,6 +1,6 @@ #import "ReactNativeVideoPlayerView.h" -#import #import "Utils.h" +#import #ifdef RCT_NEW_ARCH_ENABLED @@ -15,7 +15,8 @@ using namespace facebook::react; -@interface ReactNativeVideoPlayerView () +@interface ReactNativeVideoPlayerView () < + RCTReactNativeVideoPlayerViewViewProtocol> @end #else @@ -28,7 +29,8 @@ @interface ReactNativeVideoPlayerView () (); ++ (ComponentDescriptorProvider)componentDescriptorProvider { + return concreteComponentDescriptorProvider< + ReactNativeVideoPlayerViewComponentDescriptor>(); } -- (instancetype)initWithFrame:(CGRect)frame -{ +- (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { - static const auto defaultProps = std::make_shared(); + static const auto defaultProps = + std::make_shared(); _props = defaultProps; _view = [[UIView alloc] initWithFrame:frame]; @@ -317,71 +322,76 @@ - (instancetype)initWithFrame:(CGRect)frame return self; } -- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps -{ - const auto &oldViewProps = *std::static_pointer_cast(_props); - const auto &newViewProps = *std::static_pointer_cast(props); - - if (oldViewProps.source.uri != newViewProps.source.uri) { - NSString * uri = [[NSString alloc] initWithUTF8String: newViewProps.source.uri.c_str()]; - NSDictionary *headers = nil; - if (newViewProps.source.headers.isObject()) { - headers = [NSMutableDictionary new]; - for (auto &pair : newViewProps.source.headers.items()) { - NSString *key = [[NSString alloc] initWithUTF8String: pair.first.c_str()]; - NSString *value = [[NSString alloc] initWithUTF8String: pair.second.c_str()]; - [headers setValue:value forKey:key]; - } +- (void)updateProps:(Props::Shared const &)props + oldProps:(Props::Shared const &)oldProps { + const auto &oldViewProps = + *std::static_pointer_cast(_props); + const auto &newViewProps = + *std::static_pointer_cast(props); + + if (oldViewProps.source.uri != newViewProps.source.uri) { + NSString *uri = + [[NSString alloc] initWithUTF8String:newViewProps.source.uri.c_str()]; + NSDictionary *headers = nil; + if (newViewProps.source.headers.isObject()) { + headers = [NSMutableDictionary new]; + for (auto &pair : newViewProps.source.headers.items()) { + NSString *key = + [[NSString alloc] initWithUTF8String:pair.first.c_str()]; + NSString *value = + [[NSString alloc] initWithUTF8String:pair.second.c_str()]; + [headers setValue:value forKey:key]; } - [self playItem:[Utils sourceToPlayItem:uri headers:headers]]; - } else if (_needReplay) { - [self seekTo:0]; - _needReplay = NO; } + [self playItem:[Utils sourceToPlayItem:uri headers:headers]]; + } else if (_needReplay) { + [self seekTo:0]; + _needReplay = NO; + } - if (oldViewProps.paused != newViewProps.paused) { - [self setPaused:newViewProps.paused]; - } + if (oldViewProps.paused != newViewProps.paused) { + [self setPaused:newViewProps.paused]; + } - if (oldViewProps.seek != newViewProps.seek) { - [self setSeek:newViewProps.seek]; - } + if (oldViewProps.seek != newViewProps.seek) { + [self setSeek:newViewProps.seek]; + } - if (oldViewProps.volume != newViewProps.volume) { - [self setVolume:newViewProps.volume]; - } + if (oldViewProps.volume != newViewProps.volume) { + [self setVolume:newViewProps.volume]; + } - if (oldViewProps.speed != newViewProps.speed) { - [self setSpeed:newViewProps.speed]; - } + if (oldViewProps.speed != newViewProps.speed) { + [self setSpeed:newViewProps.speed]; + } - if (oldViewProps.muted != newViewProps.muted) { - [self setMuted:newViewProps.muted]; - } + if (oldViewProps.muted != newViewProps.muted) { + [self setMuted:newViewProps.muted]; + } - if (oldViewProps.loop != newViewProps.loop) { - [self setLoop:newViewProps.loop]; - } + if (oldViewProps.loop != newViewProps.loop) { + [self setLoop:newViewProps.loop]; + } - if (oldViewProps.resizeMode != newViewProps.resizeMode) { - if (newViewProps.resizeMode == "stretch") { - [self setLayerGravity:AVLayerVideoGravityResize]; - } else if (newViewProps.resizeMode == "cover") { - [self setLayerGravity:AVLayerVideoGravityResizeAspectFill]; - } else { - [self setLayerGravity:AVLayerVideoGravityResizeAspect]; - } + if (oldViewProps.resizeMode != newViewProps.resizeMode) { + if (newViewProps.resizeMode == "stretch") { + [self setLayerGravity:AVLayerVideoGravityResize]; + } else if (newViewProps.resizeMode == "cover") { + [self setLayerGravity:AVLayerVideoGravityResizeAspectFill]; + } else { + [self setLayerGravity:AVLayerVideoGravityResizeAspect]; } + } - if (oldViewProps.progressUpdateInterval != newViewProps.progressUpdateInterval) { - [self setProgressUpdateInterval:newViewProps.progressUpdateInterval]; - } + if (oldViewProps.progressUpdateInterval != + newViewProps.progressUpdateInterval) { + [self setProgressUpdateInterval:newViewProps.progressUpdateInterval]; + } - [super updateProps:props oldProps:oldProps]; + [super updateProps:props oldProps:oldProps]; } -- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args -{ +- (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args { if ([commandName isEqualToString:@"play"]) { [self play]; } else if ([commandName isEqualToString:@"pause"]) { @@ -393,72 +403,71 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args } } -- (void)emitOnReady -{ +- (void)emitOnReady { if (_eventEmitter) { - std::dynamic_pointer_cast(_eventEmitter) - ->onReadyForDisplay(ReactNativeVideoPlayerViewEventEmitter::OnReadyForDisplay{}); + std::dynamic_pointer_cast( + _eventEmitter) + ->onReadyForDisplay( + ReactNativeVideoPlayerViewEventEmitter::OnReadyForDisplay{}); } } -- (void)emitOnLoad -{ +- (void)emitOnLoad { if (_eventEmitter) { - std::dynamic_pointer_cast(_eventEmitter) - ->onLoad(ReactNativeVideoPlayerViewEventEmitter::OnLoad{}); + std::dynamic_pointer_cast( + _eventEmitter) + ->onLoad(ReactNativeVideoPlayerViewEventEmitter::OnLoad{}); } } -- (void)emitOnError:(NSError *)error -{ +- (void)emitOnError:(NSError *)error { if (_eventEmitter) { - std::dynamic_pointer_cast(_eventEmitter) - ->onError(ReactNativeVideoPlayerViewEventEmitter::OnError{ - .message = error.localizedDescription.UTF8String, - }); + std::dynamic_pointer_cast( + _eventEmitter) + ->onError(ReactNativeVideoPlayerViewEventEmitter::OnError{ + .message = error.localizedDescription.UTF8String, + }); } } -- (void)emitOnEnd -{ +- (void)emitOnEnd { if (_eventEmitter) { - std::dynamic_pointer_cast(_eventEmitter) - ->onEnd(ReactNativeVideoPlayerViewEventEmitter::OnEnd{}); + std::dynamic_pointer_cast( + _eventEmitter) + ->onEnd(ReactNativeVideoPlayerViewEventEmitter::OnEnd{}); } } -- (void)emitOnProgress:(Float64)currentTime duration:(Float64)duration -{ +- (void)emitOnProgress:(Float64)currentTime duration:(Float64)duration { if (_eventEmitter) { - std::dynamic_pointer_cast(_eventEmitter) - ->onProgress(ReactNativeVideoPlayerViewEventEmitter::OnProgress{ - .currentTime = currentTime, - .duration = duration, - }); + std::dynamic_pointer_cast( + _eventEmitter) + ->onProgress(ReactNativeVideoPlayerViewEventEmitter::OnProgress{ + .currentTime = currentTime, + .duration = duration, + }); } } -- (void)emitOnBuffer:(BOOL)buffering -{ +- (void)emitOnBuffer:(BOOL)buffering { if (_eventEmitter) { - std::dynamic_pointer_cast(_eventEmitter) - ->onBuffer(ReactNativeVideoPlayerViewEventEmitter::OnBuffer{ - .isBuffering = buffering, - }); + std::dynamic_pointer_cast( + _eventEmitter) + ->onBuffer(ReactNativeVideoPlayerViewEventEmitter::OnBuffer{ + .isBuffering = buffering, + }); } } -Class ReactNativeVideoPlayerViewCls(void) -{ - return ReactNativeVideoPlayerView.class; +Class ReactNativeVideoPlayerViewCls(void) { + return ReactNativeVideoPlayerView.class; } #else #pragma mark - Old Arch Impl -- (instancetype)initWithBridge:(RCTBridge *)bridge -{ +- (instancetype)initWithBridge:(RCTBridge *)bridge { if ((self = [super init])) { _bridge = bridge; [self initCommon:self]; @@ -466,18 +475,17 @@ - (instancetype)initWithBridge:(RCTBridge *)bridge return self; } -- (void)setSource:(NSDictionary *)source -{ +- (void)setSource:(NSDictionary *)source { if (source == nil) { return; } - NSString * uri = [[NSString alloc] initWithUTF8String: [source[@"uri"] UTF8String]]; + NSString *uri = + [[NSString alloc] initWithUTF8String:[source[@"uri"] UTF8String]]; NSDictionary *headers = source[@"headers"]; [self playItem:[Utils sourceToPlayItem:uri headers:headers]]; } -- (void)setResizeMode:(NSString *)resizeMode -{ +- (void)setResizeMode:(NSString *)resizeMode { if ([resizeMode isEqualToString:@"stretch"]) { [self setLayerGravity:AVLayerVideoGravityResize]; } else if ([resizeMode isEqualToString:@"cover"]) { @@ -487,51 +495,45 @@ - (void)setResizeMode:(NSString *)resizeMode } } -- (void)emitOnReady -{ +- (void)emitOnReady { if (self.onReadyForDisplay) { self.onReadyForDisplay(nil); } } -- (void)emitOnLoad -{ +- (void)emitOnLoad { if (self.onLoad) { self.onLoad(nil); } } -- (void)emitOnBuffer:(BOOL)buffering -{ +- (void)emitOnBuffer:(BOOL)buffering { if (self.onBuffer) { self.onBuffer(@{ - @"isBuffering": @(buffering), + @"isBuffering" : @(buffering), }); } } -- (void)emitOnError:(NSError *)error -{ +- (void)emitOnError:(NSError *)error { if (self.onError) { self.onError(@{ - @"message": error.localizedDescription, + @"message" : error.localizedDescription, }); } } -- (void)emitOnEnd -{ +- (void)emitOnEnd { if (self.onEnd) { self.onEnd(nil); } } -- (void)emitOnProgress:(Float64)currentTime duration:(Float64)duration -{ +- (void)emitOnProgress:(Float64)currentTime duration:(Float64)duration { if (self.onProgress) { self.onProgress(@{ - @"currentTime": @(currentTime), - @"duration": @(duration), + @"currentTime" : @(currentTime), + @"duration" : @(duration), }); } } diff --git a/ios/ReactNativeVideoPlayerViewManager.mm b/ios/ReactNativeVideoPlayerViewManager.mm index 1230971..b7629db 100644 --- a/ios/ReactNativeVideoPlayerViewManager.mm +++ b/ios/ReactNativeVideoPlayerViewManager.mm @@ -1,7 +1,7 @@ -#import -#import #import "RCTBridge.h" #import "ReactNativeVideoPlayerView.h" +#import +#import @interface ReactNativeVideoPlayerViewManager : RCTViewManager @end @@ -10,8 +10,7 @@ @implementation ReactNativeVideoPlayerViewManager RCT_EXPORT_MODULE(ReactNativeVideoPlayerView) -- (UIView *)view -{ +- (UIView *)view { #ifdef RCT_NEW_ARCH_ENABLED return [[ReactNativeVideoPlayerView alloc] init]; #else @@ -36,52 +35,70 @@ - (UIView *)view RCT_EXPORT_VIEW_PROPERTY(onBuffer, RCTDirectEventBlock); RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock); -RCT_EXPORT_METHOD(play:(nonnull NSNumber *)reactTag) -{ - [self.bridge.uiManager prependUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { - ReactNativeVideoPlayerView *view = (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; - if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { - RCTLogError(@"Invalid view returned from registry, expecting ReactNativeVideoPlayerView, got: %@", view); - } else { - [view play]; - } - }]; +RCT_EXPORT_METHOD(play : (nonnull NSNumber *)reactTag) { + [self.bridge.uiManager + prependUIBlock:^(RCTUIManager *uiManager, + NSDictionary *viewRegistry) { + ReactNativeVideoPlayerView *view = + (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; + if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { + RCTLogError(@"Invalid view returned from registry, expecting " + @"ReactNativeVideoPlayerView, got: %@", + view); + } else { + [view play]; + } + }]; } -RCT_EXPORT_METHOD(pause:(nonnull NSNumber *)reactTag) -{ - [self.bridge.uiManager prependUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { - ReactNativeVideoPlayerView *view = (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; - if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { - RCTLogError(@"Invalid view returned from registry, expecting ReactNativeVideoPlayerView, got: %@", view); - } else { - [view pause]; - } - }]; +RCT_EXPORT_METHOD(pause : (nonnull NSNumber *)reactTag) { + [self.bridge.uiManager + prependUIBlock:^(RCTUIManager *uiManager, + NSDictionary *viewRegistry) { + ReactNativeVideoPlayerView *view = + (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; + if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { + RCTLogError(@"Invalid view returned from registry, expecting " + @"ReactNativeVideoPlayerView, got: %@", + view); + } else { + [view pause]; + } + }]; } -RCT_EXPORT_METHOD(seek:(nonnull NSNumber *)reactTag time:(nonnull NSNumber *)time) -{ - [self.bridge.uiManager prependUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { - ReactNativeVideoPlayerView *view = (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; - if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { - RCTLogError(@"Invalid view returned from registry, expecting ReactNativeVideoPlayerView, got: %@", view); - } else { - [view seekTo:[time doubleValue]]; - } - }]; +RCT_EXPORT_METHOD(seek + : (nonnull NSNumber *)reactTag time + : (nonnull NSNumber *)time) { + [self.bridge.uiManager + prependUIBlock:^(RCTUIManager *uiManager, + NSDictionary *viewRegistry) { + ReactNativeVideoPlayerView *view = + (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; + if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { + RCTLogError(@"Invalid view returned from registry, expecting " + @"ReactNativeVideoPlayerView, got: %@", + view); + } else { + [view seekTo:[time doubleValue]]; + } + }]; } -RCT_EXPORT_METHOD(stop:(nonnull NSNumber *)reactTag) -{ - [self.bridge.uiManager prependUIBlock:^(RCTUIManager *uiManager, NSDictionary *viewRegistry) { - ReactNativeVideoPlayerView *view = (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; - if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { - RCTLogError(@"Invalid view returned from registry, expecting ReactNativeVideoPlayerView, got: %@", view); - } else { - [view stop]; - } - }]; +RCT_EXPORT_METHOD(stop : (nonnull NSNumber *)reactTag) { + [self.bridge.uiManager + prependUIBlock:^(RCTUIManager *uiManager, + NSDictionary *viewRegistry) { + ReactNativeVideoPlayerView *view = + (ReactNativeVideoPlayerView *)viewRegistry[reactTag]; + if (![view isKindOfClass:[ReactNativeVideoPlayerView class]]) { + RCTLogError(@"Invalid view returned from registry, expecting " + @"ReactNativeVideoPlayerView, got: %@", + view); + } else { + [view stop]; + } + }]; } @end diff --git a/ios/Utils.m b/ios/Utils.m index e0adf80..a800dcf 100644 --- a/ios/Utils.m +++ b/ios/Utils.m @@ -3,8 +3,7 @@ @implementation Utils -+ sourceToPlayItem:(NSString *)uri headers:(NSDictionary *)headers -{ ++ sourceToPlayItem:(NSString *)uri headers:(NSDictionary *)headers { if (uri == nil) { return nil; } @@ -15,14 +14,18 @@ @implementation Utils if (url == nil) { return nil; } - AVURLAsset *asset = headers == nil - ? [AVURLAsset URLAssetWithURL:url options:nil] - : [AVURLAsset URLAssetWithURL:url options:@{@"AVURLAssetHTTPHeaderFieldsKey": headers}]; + AVURLAsset *asset = + headers == nil + ? [AVURLAsset URLAssetWithURL:url options:nil] + : [AVURLAsset + URLAssetWithURL:url + options:@{@"AVURLAssetHTTPHeaderFieldsKey" : headers}]; return [AVPlayerItem playerItemWithAsset:asset]; } + (id)alloc { - [NSException raise:@"Cannot be instantiated!" format:@"Static class 'Utils' cannot be instantiated!"]; + [NSException raise:@"Cannot be instantiated!" + format:@"Static class 'Utils' cannot be instantiated!"]; return nil; }