Skip to content

Commit 3fd5d4d

Browse files
authored
support silence prop for video component (#996)
1 parent da433a3 commit 3fd5d4d

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

example/src/VideoPlayerExample.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ const VideoPlayerExample: React.FC = () => {
2424
ref={videoPlayerRef}
2525
style={{ width: 350, height: 250 }}
2626
source={{
27-
uri: "https://d23dyxeqlo5psv.cloudfront.net/big_buck_bunny.mp4",
27+
uri: "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
2828
}}
2929
useNativeControls={false}
3030
posterSource={{
31-
uri: "https://fujifilm-x.com/wp-content/uploads/2021/01/gfx100s_sample_04_thum-1.jpg",
31+
uri: "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg",
3232
}}
3333
usePoster
3434
onPlaybackStatusUpdate={(status) => setPlayerState(status)}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
AVPlaybackStatus,
88
VideoFullscreenUpdate,
99
AVPlaybackSource,
10+
Audio,
1011
} from "expo-av";
1112
import { extractSizeStyles } from "../../../utilities";
1213
import MediaPlaybackWrapper from "../MediaPlaybackWrapper";
@@ -27,6 +28,7 @@ type ExpoVideoPropsOmitted = Omit<
2728
interface VideoPlayerProps extends ExpoVideoPropsOmitted, MediaPlayerProps {
2829
resizeMode?: ResizeMode;
2930
posterResizeMode?: ImageResizeMode;
31+
playsInSilentModeIOS?: boolean;
3032
}
3133

3234
export interface VideoPlayerRef extends MediaPlayerRef {
@@ -42,6 +44,7 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
4244
onPlaybackStatusUpdate: onPlaybackStatusUpdateProp,
4345
onPlaybackFinish,
4446
source,
47+
playsInSilentModeIOS = false,
4548
...rest
4649
},
4750
ref
@@ -102,6 +105,20 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
102105
}
103106
}, [isFullscreen, videoMediaObject]);
104107

108+
const updateAudioMode = React.useCallback(async () => {
109+
try {
110+
await Audio.setAudioModeAsync({
111+
playsInSilentModeIOS,
112+
});
113+
} catch (e) {
114+
console.error("Failed to set audio mode. Error details:", e);
115+
}
116+
}, [playsInSilentModeIOS]);
117+
118+
React.useEffect(() => {
119+
updateAudioMode();
120+
}, [updateAudioMode]);
121+
105122
React.useImperativeHandle(
106123
ref,
107124
() => ({
@@ -131,6 +148,7 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
131148
media={videoMediaObject as Playback | undefined}
132149
isPlaying={isPlaying}
133150
ref={mediaPlaybackWrapperRef}
151+
onTogglePlayback={updateAudioMode}
134152
>
135153
<VideoPlayerComponent
136154
// https://docs.expo.dev/versions/latest/sdk/av/#example-video to see why ref is handled this way

0 commit comments

Comments
 (0)