Skip to content

Commit 859b6ea

Browse files
authored
support silence for video component - 52 (#1002)
1 parent 2a651e9 commit 859b6ea

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

example/src/VideoPlayerExample.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ const VideoPlayerExample: React.FC = () => {
1919
resizeMode="cover"
2020
/>
2121
</Section>
22+
<Section style={{}} title="VideoPlayer (Silent mode)">
23+
<VideoPlayer
24+
style={{ width: 350, height: 250 }}
25+
source={{
26+
uri: "http://static.draftbit.com/videos/intro-to-draftbit.mp4",
27+
}}
28+
useNativeControls
29+
resizeMode="cover"
30+
playsInSilentModeIOS={true}
31+
/>
32+
</Section>
2233
<Section style={{}} title="VideoPlayer (Poster and custom controls)">
2334
<VideoPlayer
2435
ref={videoPlayerRef}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { ImageResizeMode } from "react-native";
2+
import { ImageResizeMode, Platform } from "react-native";
33
import {
44
Video as VideoPlayerComponent,
55
VideoProps as ExpoVideoProps,
@@ -35,6 +35,18 @@ export interface VideoPlayerRef extends MediaPlayerRef {
3535
toggleFullscreen: () => void;
3636
}
3737

38+
// Setting playsInSilentModeIOS prop directly on Video component is unreliable,
39+
// so we need to set the audio mode globally before playing.
40+
// See:
41+
// https://github.com/expo/expo/issues/7485
42+
// https://stackoverflow.com/questions/57371543/how-to-fix-video-play-but-dont-have-sound-on-ios-with-expo
43+
const triggerAudio = async (ref: React.RefObject<MediaPlayerRef>) => {
44+
if (ref && ref?.current && Platform.OS === "ios") {
45+
await Audio.setAudioModeAsync({ playsInSilentModeIOS: true });
46+
ref.current.play();
47+
}
48+
};
49+
3850
const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
3951
(
4052
{
@@ -116,8 +128,8 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
116128
}, [playsInSilentModeIOS]);
117129

118130
React.useEffect(() => {
119-
updateAudioMode();
120-
}, [updateAudioMode]);
131+
if (isPlaying) triggerAudio(mediaPlaybackWrapperRef);
132+
}, [mediaPlaybackWrapperRef, isPlaying]);
121133

122134
React.useImperativeHandle(
123135
ref,

0 commit comments

Comments
 (0)