Skip to content

Commit

Permalink
feat: sync behavior with other video player
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Aug 31, 2023
1 parent 68e06aa commit 48a0973
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class ReactNativeVideoPlayerView : FrameLayout, SurfaceHolder.Callback,
return
}
fireEvent("progress", Arguments.createMap().apply {
putDouble("position", (player?.currentPosition ?: 0).toDouble() / 1000)
putDouble("currentTime", (player?.currentPosition ?: 0).toDouble() / 1000)
putDouble("duration", (player?.duration ?: 0).toDouble() / 1000)
})
if (player?.isPlaying == true) {
Expand Down
6 changes: 3 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export default function App() {
console.log(`[ready-${i}] ${performance.now()}`)
}
onLoad={() => console.log(`[load-${i}] ${performance.now()}`)}
onProgress={({ nativeEvent: { position, duration } }) =>
console.log(`[progress-${i}] ${position} / ${duration}`)
onProgress={({ currentTime, duration }) =>
console.log(`[progress-${i}] ${currentTime} / ${duration}`)
}
onEnd={next}
onError={({ nativeEvent }) => console.error(nativeEvent)}
onError={(err) => console.error(err)}
/>
)}
</View>
Expand Down
8 changes: 4 additions & 4 deletions ios/ReactNativeVideoPlayerView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ - (void)emitOnEnd
}
}

- (void)emitOnProgress:(float)position duration:(float)duration
- (void)emitOnProgress:(float)currentTime duration:(float)duration
{
if (_eventEmitter) {
std::dynamic_pointer_cast<const ReactNativeVideoPlayerViewEventEmitter>(_eventEmitter)
->onProgress(ReactNativeVideoPlayerViewEventEmitter::OnProgress{
.position = position,
.currentTime = currentTime,
.duration = duration,
});
}
Expand Down Expand Up @@ -456,11 +456,11 @@ - (void)emitOnEnd
}
}

- (void)emitOnProgress:(float)position duration:(float)duration
- (void)emitOnProgress:(float)currentTime duration:(float)duration
{
if (self.onProgress) {
self.onProgress(@{
@"position": @(position),
@"currentTime": @(currentTime),
@"duration": @(duration),
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/ReactNativeVideoPlayerViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from 'react-native/Libraries/Types/CodegenTypes';

export type ProgressEvent = Readonly<{
position: Float;
currentTime: Float;
duration: Float;
}>;

Expand Down
65 changes: 62 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import React, { useImperativeHandle, forwardRef, useRef } from 'react';
import React, {
useImperativeHandle,
forwardRef,
useRef,
useCallback,
} from 'react';
import Player, { Commands } from './ReactNativeVideoPlayerViewNativeComponent';
import type { VideoPlayerProps } from './ReactNativeVideoPlayerViewNativeComponent';
export * from './ReactNativeVideoPlayerViewNativeComponent';

type PlayerRef = InstanceType<typeof Player>;
type Modify<T, R> = Omit<T, keyof R> & R;

export default forwardRef(function VideoPlayer(props: VideoPlayerProps, ref) {
export type Props = Modify<
VideoPlayerProps,
{
onReadyForDisplay?: () => void;
onLoad?: () => void;
onProgress?: (event: { currentTime: number; duration: number }) => void;
onEnd?: () => void;
onError?: (event: { message: string }) => void;
onBuffer?: (event: { isBuffering: boolean }) => void;
}
>;

export default forwardRef(function VideoPlayer(props: Props, ref) {
const {
onReadyForDisplay,
onLoad,
onProgress,
onEnd,
onError,
onBuffer,
...rest
} = props;
const nativeRef = useRef<PlayerRef>(null);

useImperativeHandle(ref, () => ({
Expand All @@ -16,5 +43,37 @@ export default forwardRef(function VideoPlayer(props: VideoPlayerProps, ref) {
stop: () => nativeRef.current && Commands.stop(nativeRef.current),
}));

return <Player {...props} ref={nativeRef} />;
return (
<Player
{...rest}
ref={nativeRef}
onReadyForDisplay={useCallback(() => {
onReadyForDisplay && onReadyForDisplay();
}, [onReadyForDisplay])}
onLoad={useCallback(() => {
onLoad && onLoad();
}, [onLoad])}
onProgress={useCallback(
(event) => {
onProgress && onProgress(event.nativeEvent);
},
[onProgress]
)}
onEnd={useCallback(() => {
onEnd && onEnd();
}, [onEnd])}
onError={useCallback(
(event) => {
onError && onError(event.nativeEvent);
},
[onError]
)}
onBuffer={useCallback(
(event) => {
onBuffer && onBuffer(event.nativeEvent);
},
[onBuffer]
)}
/>
);
});

0 comments on commit 48a0973

Please sign in to comment.