From 1327fc622f7521269f66299c3aca610494c76fe1 Mon Sep 17 00:00:00 2001 From: Brian Sztamfater Date: Tue, 15 Aug 2023 13:25:28 -0300 Subject: [PATCH] feat: add loop property for iOS (#3) --- ios/TransparentVideoViewManager.m | 1 + ios/TransparentVideoViewManager.swift | 14 +++++++++++--- src/index.tsx | 2 ++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ios/TransparentVideoViewManager.m b/ios/TransparentVideoViewManager.m index 2d3a000..3ca342c 100644 --- a/ios/TransparentVideoViewManager.m +++ b/ios/TransparentVideoViewManager.m @@ -3,5 +3,6 @@ @interface RCT_EXTERN_MODULE(TransparentVideoViewManager, RCTViewManager) RCT_EXPORT_VIEW_PROPERTY(src, NSDictionary); +RCT_EXPORT_VIEW_PROPERTY(loop, BOOL); @end diff --git a/ios/TransparentVideoViewManager.swift b/ios/TransparentVideoViewManager.swift index cbcaca3..bf6c84a 100644 --- a/ios/TransparentVideoViewManager.swift +++ b/ios/TransparentVideoViewManager.swift @@ -26,6 +26,17 @@ class TransparentVideoView : UIView { } } + @objc var loop: Bool = Bool() { + didSet { + // Setup looping on our video + self.playerView?.isLoopingEnabled = loop + let player = self.playerView?.player + if (loop && (player?.rate == 0 || player?.error != nil)) { + player?.play() + } + } + } + func loadVideoPlayer(itemUrl: URL) { if (self.playerView == nil) { let playerView = AVPlayerView(frame: CGRect(origin: .zero, size: .zero)) @@ -44,9 +55,6 @@ class TransparentVideoView : UIView { let playerLayer: AVPlayerLayer = playerView.playerLayer playerLayer.pixelBufferAttributes = [ (kCVPixelBufferPixelFormatTypeKey as String): kCVPixelFormatType_32BGRA] - - // Setup looping on our video - playerView.isLoopingEnabled = true NotificationCenter.default.addObserver(self, selector: #selector(appEnteredBackgound), name: UIApplication.didEnterBackgroundNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(appEnteredForeground), name: UIApplication.willEnterForegroundNotification, object: nil) diff --git a/src/index.tsx b/src/index.tsx index 1eb0f20..a0df4db 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,6 +6,7 @@ import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource' type TransparentVideoProps = { style: ViewStyle; source?: any; + loop?: boolean; }; const ComponentName = 'TransparentVideoView'; @@ -27,6 +28,7 @@ class TransparentVideo extends React.PureComponent { uri, type: source.type || '', }, + loop: nativeProps.loop ?? true, }); return ;