@@ -8,7 +8,11 @@ import React, {
88} from 'react' ;
99import { View , Image , Platform } from 'react-native' ;
1010
11- import type { AudioHandle , AudioProps , AudioTagPlaybackState } from './types' ;
11+ import type {
12+ AudioTagHandle ,
13+ AudioProps ,
14+ AudioTagPlaybackState ,
15+ } from './types' ;
1216
1317import { AudioComponentContext } from './AudioTagContext' ;
1418import { AudioFileSourceNode } from './AudioFileSourceNode' ;
@@ -17,7 +21,7 @@ import { NotSupportedError } from '../../../errors';
1721import { NativeAudioAPIModule } from '../../../specs' ;
1822import { AudioControls } from '..' ;
1923
20- const Audio = React . forwardRef < AudioHandle , AudioProps > ( ( props , ref ) => {
24+ const Audio = React . forwardRef < AudioTagHandle , AudioProps > ( ( props , ref ) => {
2125 const { children } = props ;
2226 const {
2327 autoPlay,
@@ -33,16 +37,15 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
3337 onLoadStart,
3438 onLoad,
3539 onError,
36- onPositionChanged,
37- onSeek,
40+ onPositionChange,
3841 onEnded : onEndedCallback ,
3942 onPlay,
4043 onPause,
4144 onVolumeChange,
4245 } = useStableAudioProps ( props ) ;
4346 const audioContext = context ?? null ;
44- const [ volumeState , setVolumeState ] = useState ( volume ) ;
45- const [ mutedState , setMutedState ] = useState ( muted ) ;
47+ const [ volumeState , setVolumeState ] = useState < number | null > ( null ) ;
48+ const [ mutedState , setMutedState ] = useState < boolean | null > ( null ) ;
4649 const [ ready , setReady ] = useState ( false ) ;
4750
4851 const path = useMemo ( ( ) => {
@@ -69,18 +72,24 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
6972 const fileSourceRef = useRef < AudioFileSourceNode > ( null ) ;
7073 const sourceRef = useRef < ArrayBuffer | string | null > ( null ) ;
7174
72- const effectiveVolumeRef = useRef ( mutedState ? 0 : volumeState ) ;
7375 const lastEffectiveVolumeRef = useRef ( muted ? 0 : volume ) ;
7476
7577 const [ playbackState , setPlaybackState ] =
7678 useState < AudioTagPlaybackState > ( 'idle' ) ;
7779 const [ currentTime , setCurrentTime ] = useState ( 0 ) ;
7880 const [ duration , setDuration ] = useState ( 0 ) ;
7981
82+ const effectiveMutedState = useMemo ( ( ) => {
83+ return mutedState ?? muted ;
84+ } , [ mutedState , muted ] ) ;
85+
86+ const effectiveVolumeState = useMemo ( ( ) => {
87+ return effectiveMutedState ? 0 : ( volumeState ?? volume ) ;
88+ } , [ effectiveMutedState , volumeState , volume ] ) ;
89+
8090 useEffect ( ( ) => {
81- effectiveVolumeRef . current = mutedState ? 0 : volumeState ;
82- fileSourceRef . current ?. setVolume ( effectiveVolumeRef . current ) ;
83- } , [ mutedState , volumeState ] ) ;
91+ fileSourceRef . current ?. setVolume ( effectiveVolumeState ) ;
92+ } , [ effectiveVolumeState ] ) ;
8493
8594 const play = useCallback ( ( ) => {
8695 fileSourceRef . current ?. play ( ) ;
@@ -102,9 +111,9 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
102111 ? Math . max ( 0 , Math . min ( seconds , duration ) )
103112 : Math . max ( 0 , seconds ) ;
104113 setCurrentTime ( nextTime ) ;
105- onSeek ( nextTime ) ;
114+ onPositionChange ( nextTime ) ;
106115 } ,
107- [ duration , onSeek , setCurrentTime ]
116+ [ duration , setCurrentTime , onPositionChange ]
108117 ) ;
109118
110119 const spawnFileSource = useCallback ( ( ) => {
@@ -121,7 +130,7 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
121130 const node = context . context . createFileSource ( {
122131 source : nextSource ,
123132 loop,
124- volume : effectiveVolumeRef . current ,
133+ volume : effectiveVolumeState ,
125134 } ) ;
126135 if ( ! node ) {
127136 onError ( new NotSupportedError ( 'This file format requires FFmpeg build' ) ) ;
@@ -139,7 +148,7 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
139148 } ,
140149 } ) ;
141150
142- fileSource . setVolume ( effectiveVolumeRef . current ) ;
151+ fileSource . setVolume ( effectiveVolumeState ) ;
143152 fileSourceRef . current = fileSource ;
144153 setDuration ( nextDuration ) ;
145154 onLoad ( ) ;
@@ -149,7 +158,16 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
149158 setPlaybackState ( 'playing' ) ;
150159 onPlay ( ) ;
151160 }
152- } , [ context , loop , onError , onEndedCallback , onLoad , onPlay , autoPlay ] ) ;
161+ } , [
162+ context ,
163+ loop ,
164+ onError ,
165+ onEndedCallback ,
166+ onLoad ,
167+ onPlay ,
168+ autoPlay ,
169+ effectiveVolumeState ,
170+ ] ) ;
153171
154172 useEffect ( ( ) => {
155173 if ( ! path ) {
@@ -205,12 +223,11 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
205223 } , [ path , source , spawnFileSource , onError , onLoadStart ] ) ;
206224
207225 useEffect ( ( ) => {
208- const effectiveVolume = mutedState ? 0 : volumeState ;
209- if ( lastEffectiveVolumeRef . current !== effectiveVolume ) {
210- lastEffectiveVolumeRef . current = effectiveVolume ;
211- onVolumeChange ( effectiveVolume ) ;
226+ if ( lastEffectiveVolumeRef . current !== effectiveVolumeState ) {
227+ lastEffectiveVolumeRef . current = effectiveVolumeState ;
228+ onVolumeChange ( effectiveVolumeState ) ;
212229 }
213- } , [ mutedState , onVolumeChange , volumeState ] ) ;
230+ } , [ onVolumeChange , effectiveVolumeState ] ) ;
214231
215232 useEffect ( ( ) => {
216233 fileSourceRef . current ?. setLoop ( loop ) ;
@@ -223,13 +240,13 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
223240
224241 fileSourceRef . current ?. startPositionTracking ( ( seconds ) => {
225242 setCurrentTime ( seconds ) ;
226- onPositionChanged ( seconds ) ;
243+ onPositionChange ( seconds ) ;
227244 } ) ;
228245
229246 return ( ) => {
230247 fileSourceRef . current ?. stopPositionTracking ( ) ;
231248 } ;
232- } , [ onPositionChanged , playbackState ] ) ;
249+ } , [ onPositionChange , playbackState ] ) ;
233250
234251 useImperativeHandle (
235252 ref ,
@@ -249,10 +266,10 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
249266 pause,
250267 seekToTime,
251268 setVolume : setVolumeState ,
252- volume : volumeState ,
269+ volume : effectiveVolumeState ,
253270 ready,
254271 setMuted : setMutedState ,
255- muted : mutedState ,
272+ muted : effectiveMutedState ,
256273 playbackState,
257274 currentTime,
258275 duration,
@@ -271,10 +288,10 @@ const Audio = React.forwardRef<AudioHandle, AudioProps>((props, ref) => {
271288 pause ,
272289 seekToTime ,
273290 setVolumeState ,
274- volumeState ,
291+ effectiveVolumeState ,
275292 ready ,
276293 setMutedState ,
277- mutedState ,
294+ effectiveMutedState ,
278295 playbackState ,
279296 currentTime ,
280297 duration ,
0 commit comments