|
1 | 1 | import React from "react";
|
2 |
| -import { ImageResizeMode } from "react-native"; |
| 2 | +import { ImageResizeMode, Platform } from "react-native"; |
3 | 3 | import {
|
4 | 4 | Video as VideoPlayerComponent,
|
5 | 5 | VideoProps as ExpoVideoProps,
|
@@ -35,6 +35,18 @@ export interface VideoPlayerRef extends MediaPlayerRef {
|
35 | 35 | toggleFullscreen: () => void;
|
36 | 36 | }
|
37 | 37 |
|
| 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 | + |
38 | 50 | const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
|
39 | 51 | (
|
40 | 52 | {
|
@@ -116,8 +128,8 @@ const VideoPlayer = React.forwardRef<VideoPlayerRef, VideoPlayerProps>(
|
116 | 128 | }, [playsInSilentModeIOS]);
|
117 | 129 |
|
118 | 130 | React.useEffect(() => {
|
119 |
| - updateAudioMode(); |
120 |
| - }, [updateAudioMode]); |
| 131 | + if (isPlaying) triggerAudio(mediaPlaybackWrapperRef); |
| 132 | + }, [mediaPlaybackWrapperRef, isPlaying]); |
121 | 133 |
|
122 | 134 | React.useImperativeHandle(
|
123 | 135 | ref,
|
|
0 commit comments