Skip to content

Commit 6bf236e

Browse files
committed
Added seperate pause/play to media components
1 parent f02c740 commit 6bf236e

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

packages/core/src/__tests__/components/AudioPlayer.test.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ describe("AudioPlayer tests", () => {
153153
expect(mockPauseAsync).toBeCalled();
154154
});
155155

156+
test("should play() play the audio", async () => {
157+
const ref = React.createRef<AudioPlayerRef>();
158+
159+
render(<AudioPlayer ref={ref} source={mockAudioSource} />);
160+
161+
await act(async () => {
162+
await waitForSoundToLoad();
163+
ref.current?.play();
164+
});
165+
expect(mockPlayAsync).toBeCalled();
166+
});
167+
168+
test("should pause() pause the audio", async () => {
169+
const ref = React.createRef<AudioPlayerRef>();
170+
171+
render(<AudioPlayer ref={ref} source={mockAudioSource} />);
172+
173+
await act(async () => {
174+
await waitForSoundToLoad();
175+
ref.current?.pause();
176+
});
177+
expect(mockPauseAsync).toBeCalled();
178+
});
179+
156180
test("should audio be cleaned up/unloaded when unmounting", async () => {
157181
render(<AudioPlayer source={mockAudioSource} />);
158182

packages/core/src/__tests__/components/VideoPlayer.test.tsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ describe("VideoPlayer tests", () => {
7070
expect(mockPauseAsync).toBeCalled();
7171
});
7272

73+
test("should play() play the video", async () => {
74+
const ref = React.createRef<VideoPlayerRef>();
75+
76+
render(
77+
<VideoPlayer
78+
ref={ref}
79+
source={{
80+
uri: "video-uri",
81+
}}
82+
/>
83+
);
84+
85+
act(() => {
86+
ref.current?.play();
87+
});
88+
expect(mockPlayAsync).toBeCalled();
89+
});
90+
91+
test("should pause() pause the video", async () => {
92+
const ref = React.createRef<VideoPlayerRef>();
93+
94+
render(
95+
<VideoPlayer
96+
ref={ref}
97+
source={{
98+
uri: "video-uri",
99+
}}
100+
/>
101+
);
102+
103+
act(() => {
104+
ref.current?.pause();
105+
});
106+
expect(mockPauseAsync).toBeCalled();
107+
});
108+
73109
test("should video be cleaned up/unloaded when unmounting", async () => {
74110
render(
75111
<VideoPlayer

packages/core/src/components/MediaPlayer/MediaPlaybackWrapper.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ const MediaPlaybackWrapper = React.forwardRef<
2626
}
2727
}, [media, isPlaying, onTogglePlayback]);
2828

29+
const pause = React.useCallback(async () => {
30+
onTogglePlayback?.();
31+
await media?.pauseAsync();
32+
}, [media, onTogglePlayback]);
33+
34+
const play = React.useCallback(async () => {
35+
onTogglePlayback?.();
36+
await media?.playAsync();
37+
}, [media, onTogglePlayback]);
38+
2939
const seekToPosition = React.useCallback(
3040
async (positionMillis: number) => {
3141
await media?.setPositionAsync(positionMillis);
@@ -46,8 +56,10 @@ const MediaPlaybackWrapper = React.forwardRef<
4656
() => ({
4757
seekToPosition,
4858
togglePlayback,
59+
pause,
60+
play,
4961
}),
50-
[seekToPosition, togglePlayback]
62+
[seekToPosition, togglePlayback, pause, play]
5163
);
5264

5365
return <>{children}</>;

packages/core/src/components/MediaPlayer/MediaPlayerCommon.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface MediaPlayerStatus {
1414
export interface MediaPlayerRef {
1515
seekToPosition: (positionMillis: number) => void;
1616
togglePlayback: () => void;
17+
pause: () => void;
18+
play: () => void;
1719
}
1820

1921
export interface MediaPlayerProps {

packages/core/src/components/MediaPlayer/VideoPlayer/VideoPlayer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
105105
mediaPlaybackWrapperRef.current?.seekToPosition || (() => {}),
106106
togglePlayback:
107107
mediaPlaybackWrapperRef.current?.togglePlayback || (() => {}),
108+
pause: mediaPlaybackWrapperRef.current?.pause || (() => {}),
109+
play: mediaPlaybackWrapperRef.current?.play || (() => {}),
108110
}),
109111
// Include 'isPlaying' as dependency because 'togglePlayback' changes when it changes
110112
// eslint-disable-next-line react-hooks/exhaustive-deps

0 commit comments

Comments
 (0)