Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions example/src/VideoPlayerExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ const VideoPlayerExample: React.FC = () => {
resizeMode="cover"
/>
</Section>
<Section style={{}} title="VideoPlayer (Silent mode)">
<VideoPlayer
style={{ width: 350, height: 250 }}
source={{
uri: "http://static.draftbit.com/videos/intro-to-draftbit.mp4",
}}
useNativeControls
resizeMode="cover"
playsInSilentModeIOS={true}
/>
</Section>
<Section style={{}} title="VideoPlayer (Poster and custom controls)">
<VideoPlayer
ref={videoPlayerRef}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { ImageResizeMode } from "react-native";
import { ImageResizeMode, Platform } from "react-native";
import {
Video as VideoPlayerComponent,
VideoProps as ExpoVideoProps,
Expand Down Expand Up @@ -35,6 +35,18 @@ export interface VideoPlayerRef extends MediaPlayerRef {
toggleFullscreen: () => void;
}

// Setting playsInSilentModeIOS prop directly on Video component is unreliable,
// so we need to set the audio mode globally before playing.
// See:
// https://github.com/expo/expo/issues/7485
// https://stackoverflow.com/questions/57371543/how-to-fix-video-play-but-dont-have-sound-on-ios-with-expo
const triggerAudio = async (ref: React.RefObject<MediaPlayerRef>) => {
if (ref && ref?.current && Platform.OS === "ios") {
await Audio.setAudioModeAsync({ playsInSilentModeIOS: true });
ref.current.play();
}
};

const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
(
{
Expand Down Expand Up @@ -116,8 +128,8 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
}, [playsInSilentModeIOS]);

React.useEffect(() => {
updateAudioMode();
}, [updateAudioMode]);
if (isPlaying) triggerAudio(mediaPlaybackWrapperRef);
}, [mediaPlaybackWrapperRef, isPlaying]);

React.useImperativeHandle(
ref,
Expand Down