1- import React from 'react' ;
1+ import React , { useCallback } from 'react' ;
22
33import { NativeHandlers , SoundReturnType } from '../native' ;
44
@@ -15,7 +15,7 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
1515
1616 const isExpoCLI = NativeHandlers . SDK === 'stream-chat-expo' ;
1717
18- const playAudio = async ( ) => {
18+ const playAudio = useCallback ( async ( ) => {
1919 if ( isExpoCLI ) {
2020 if ( soundRef . current ?. playAsync ) {
2121 await soundRef . current . playAsync ( ) ;
@@ -25,9 +25,9 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
2525 soundRef . current . resume ( ) ;
2626 }
2727 }
28- } ;
28+ } , [ isExpoCLI , soundRef ] ) ;
2929
30- const pauseAudio = async ( ) => {
30+ const pauseAudio = useCallback ( async ( ) => {
3131 if ( isExpoCLI ) {
3232 if ( soundRef . current ?. pauseAsync ) {
3333 await soundRef . current . pauseAsync ( ) ;
@@ -37,39 +37,45 @@ export const useAudioPlayer = (props: UseSoundPlayerProps) => {
3737 soundRef . current . pause ( ) ;
3838 }
3939 }
40- } ;
40+ } , [ isExpoCLI , soundRef ] ) ;
4141
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+ }
5157 }
5258 } else {
53- if ( soundRef . current ?. setPositionAsync ) {
54- await soundRef . current . setPositionAsync ( currentTime * 1000 ) ;
59+ if ( soundRef . current ?. seek ) {
60+ soundRef . current . seek ( currentTime ) ;
5561 }
5662 }
57- } else {
58- if ( soundRef . current ?. seek ) {
59- soundRef . current . seek ( currentTime ) ;
60- }
61- }
62- } ;
63+ } ,
64+ [ isExpoCLI , soundRef ] ,
65+ ) ;
6366
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+ ) ;
7379
7480 return { changeAudioSpeed, pauseAudio, playAudio, seekAudio } ;
7581} ;
0 commit comments