1
- import React from 'react' ;
1
+ import React , { useCallback } from 'react' ;
2
2
3
3
import { NativeHandlers , SoundReturnType } from '../native' ;
4
4
@@ -15,7 +15,7 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
15
15
16
16
const isExpoCLI = NativeHandlers . SDK === 'stream-chat-expo' ;
17
17
18
- const playAudio = async ( ) => {
18
+ const playAudio = useCallback ( async ( ) => {
19
19
if ( isExpoCLI ) {
20
20
if ( soundRef . current ?. playAsync ) {
21
21
await soundRef . current . playAsync ( ) ;
@@ -25,9 +25,9 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
25
25
soundRef . current . resume ( ) ;
26
26
}
27
27
}
28
- } ;
28
+ } , [ isExpoCLI , soundRef ] ) ;
29
29
30
- const pauseAudio = async ( ) => {
30
+ const pauseAudio = useCallback ( async ( ) => {
31
31
if ( isExpoCLI ) {
32
32
if ( soundRef . current ?. pauseAsync ) {
33
33
await soundRef . current . pauseAsync ( ) ;
@@ -37,39 +37,45 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
37
37
soundRef . current . pause ( ) ;
38
38
}
39
39
}
40
- } ;
40
+ } , [ isExpoCLI , soundRef ] ) ;
41
41
42
- const seekAudio = async ( currentTime : number ) => {
43
- if ( isExpoCLI ) {
44
- if ( currentTime === 0 ) {
45
- // If currentTime is 0, we should replay the video from 0th position.
46
- if ( soundRef . current ?. replayAsync ) {
47
- await soundRef . current . replayAsync ( {
48
- positionMillis : 0 ,
49
- shouldPlay : false ,
50
- } ) ;
42
+ const seekAudio = useCallback (
43
+ async ( currentTime : number ) => {
44
+ if ( isExpoCLI ) {
45
+ if ( currentTime === 0 ) {
46
+ // If currentTime is 0, we should replay the video from 0th position.
47
+ if ( soundRef . current ?. replayAsync ) {
48
+ await soundRef . current . replayAsync ( {
49
+ positionMillis : 0 ,
50
+ shouldPlay : false ,
51
+ } ) ;
52
+ }
53
+ } else {
54
+ if ( soundRef . current ?. setPositionAsync ) {
55
+ await soundRef . current . setPositionAsync ( currentTime * 1000 ) ;
56
+ }
51
57
}
52
58
} else {
53
- if ( soundRef . current ?. setPositionAsync ) {
54
- await soundRef . current . setPositionAsync ( currentTime * 1000 ) ;
59
+ if ( soundRef . current ?. seek ) {
60
+ soundRef . current . seek ( currentTime ) ;
55
61
}
56
62
}
57
- } else {
58
- if ( soundRef . current ?. seek ) {
59
- soundRef . current . seek ( currentTime ) ;
60
- }
61
- }
62
- } ;
63
+ } ,
64
+ [ isExpoCLI , soundRef ] ,
65
+ ) ;
63
66
64
- const changeAudioSpeed = async ( speed : number ) => {
65
- // Handled through prop `rate` in `Sound.Player`
66
- if ( ! isExpoCLI ) {
67
- return ;
68
- }
69
- if ( soundRef . current ?. setRateAsync ) {
70
- await soundRef . current . setRateAsync ( speed ) ;
71
- }
72
- } ;
67
+ const changeAudioSpeed = useCallback (
68
+ async ( speed : number ) => {
69
+ // Handled through prop `rate` in `Sound.Player`
70
+ if ( ! isExpoCLI ) {
71
+ return ;
72
+ }
73
+ if ( soundRef . current ?. setRateAsync ) {
74
+ await soundRef . current . setRateAsync ( speed ) ;
75
+ }
76
+ } ,
77
+ [ isExpoCLI , soundRef ] ,
78
+ ) ;
73
79
74
80
return { changeAudioSpeed, pauseAudio, playAudio, seekAudio } ;
75
81
} ;
0 commit comments