@@ -44,18 +44,15 @@ import { Colors } from './theme';
4444import FastImage from 'react-native-fast-image' ;
4545import fs from 'react-native-fs' ;
4646
47+ let currentPlayingRef : React . RefObject < IWaveformRef > | undefined ;
4748const RenderListItem = React . memo (
4849 ( {
4950 item,
50- currentPlaying,
51- setCurrentPlaying,
5251 onPanStateChange,
5352 currentPlaybackSpeed,
5453 changeSpeed,
5554 } : {
5655 item : ListItem ;
57- currentPlaying : string ;
58- setCurrentPlaying : Dispatch < SetStateAction < string > > ;
5956 onPanStateChange : ( value : boolean ) => void ;
6057 currentPlaybackSpeed : PlaybackSpeedType ;
6158 changeSpeed : ( ) => void ;
@@ -65,35 +62,57 @@ const RenderListItem = React.memo(
6562 const styles = stylesheet ( { currentUser : item . fromCurrentUser } ) ;
6663 const [ isLoading , setIsLoading ] = useState ( true ) ;
6764
68- const handleButtonAction = ( ) => {
69- if ( playerState === PlayerState . stopped ) {
70- setCurrentPlaying ( item . path ) ;
71- } else if ( playerState === PlayerState . playing ) {
72- ref . current ?. pausePlayer ( ) ;
73- } else {
74- ref . current ?. resumePlayer ( ) ;
65+ const handlePlayPauseAction = async ( ) => {
66+ // If we are recording do nothing
67+ if (
68+ currentPlayingRef ?. current ?. currentState === RecorderState . recording
69+ ) {
70+ return ;
7571 }
76- } ;
7772
78- const handleStopAction = ( ) => {
79- setCurrentPlaying ( '' ) ;
80- } ;
73+ const startNewPlayer = async ( ) => {
74+ currentPlayingRef = ref ;
75+ if ( ref . current ?. currentState === PlayerState . paused ) {
76+ await ref . current ?. resumePlayer ( ) ;
77+ } else {
78+ await ref . current ?. startPlayer ( {
79+ finishMode : FinishMode . stop ,
80+ } ) ;
81+ }
82+ } ;
8183
82- useEffect ( ( ) => {
83- if ( currentPlaying !== item . path ) {
84- ref . current ?. stopPlayer ( ) ;
84+ // If no player or if current player is stopped just start the new player!
85+ if (
86+ currentPlayingRef == null ||
87+ [ PlayerState . stopped , PlayerState . paused ] . includes (
88+ currentPlayingRef ?. current ?. currentState as PlayerState
89+ )
90+ ) {
91+ await startNewPlayer ( ) ;
8592 } else {
86- ref . current ?. startPlayer ( { finishMode : FinishMode . stop } ) ;
93+ // Pause current player if it was playing
94+ if ( currentPlayingRef ?. current ?. currentState === PlayerState . playing ) {
95+ await currentPlayingRef ?. current ?. pausePlayer ( ) ;
96+ }
97+
98+ // Start player when it is a different one!
99+ if ( currentPlayingRef ?. current ?. playerKey !== ref ?. current ?. playerKey ) {
100+ await startNewPlayer ( ) ;
101+ }
87102 }
88- } , [ currentPlaying ] ) ;
103+ } ;
104+
105+ const handleStopAction = async ( ) => {
106+ ref . current ?. stopPlayer ( ) ;
107+ } ;
89108
90109 return (
91110 < View key = { item . path } style = { [ styles . listItemContainer ] } >
92111 < View style = { styles . listItemWidth } >
93112 < View style = { [ styles . buttonContainer ] } >
94113 < Pressable
95114 disabled = { isLoading }
96- onPress = { handleButtonAction }
115+ onPress = { handlePlayPauseAction }
97116 style = { styles . playBackControlPressable } >
98117 { isLoading ? (
99118 < ActivityIndicator color = { '#FFFFFF' } />
@@ -140,25 +159,14 @@ const RenderListItem = React.memo(
140159 scrubColor = { Colors . white }
141160 waveColor = { Colors . lightWhite }
142161 candleHeightScale = { 4 }
143- onPlayerStateChange = { state => {
144- setPlayerState ( state ) ;
145- if (
146- state === PlayerState . stopped &&
147- currentPlaying === item . path
148- ) {
149- setCurrentPlaying ( '' ) ;
150- }
151- } }
162+ onPlayerStateChange = { setPlayerState }
152163 onPanStateChange = { onPanStateChange }
153164 onError = { error => {
154165 console . log ( error , 'we are in example' ) ;
155166 } }
156167 onCurrentProgressChange = { ( currentProgress , songDuration ) => {
157168 console . log (
158- 'currentProgress ' ,
159- currentProgress ,
160- 'songDuration ' ,
161- songDuration
169+ `currentProgress ${ currentProgress } , songDuration ${ songDuration } `
162170 ) ;
163171 } }
164172 onChangeWaveformLoadState = { state => {
@@ -203,13 +211,20 @@ const LivePlayerComponent = ({
203211
204212 const handleRecorderAction = async ( ) => {
205213 if ( recorderState === RecorderState . stopped ) {
206- let hasPermission = await checkHasAudioRecorderPermission ( ) ;
214+ // Stopping other player before starting recording
215+ if ( currentPlayingRef ?. current ?. currentState === PlayerState . playing ) {
216+ currentPlayingRef ?. current ?. stopPlayer ( ) ;
217+ }
218+
219+ const hasPermission = await checkHasAudioRecorderPermission ( ) ;
207220
208221 if ( hasPermission === PermissionStatus . granted ) {
222+ currentPlayingRef = ref ;
209223 startRecording ( ) ;
210224 } else if ( hasPermission === PermissionStatus . undetermined ) {
211225 const permissionStatus = await getAudioRecorderPermission ( ) ;
212226 if ( permissionStatus === PermissionStatus . granted ) {
227+ currentPlayingRef = ref ;
213228 startRecording ( ) ;
214229 }
215230 } else {
@@ -219,6 +234,7 @@ const LivePlayerComponent = ({
219234 ref . current ?. stopRecord ( ) . then ( path => {
220235 setList ( prev => [ ...prev , { fromCurrentUser : true , path } ] ) ;
221236 } ) ;
237+ currentPlayingRef = undefined ;
222238 }
223239 } ;
224240
@@ -250,7 +266,6 @@ const LivePlayerComponent = ({
250266
251267const AppContainer = ( ) => {
252268 const [ shouldScroll , setShouldScroll ] = useState < boolean > ( true ) ;
253- const [ currentPlaying , setCurrentPlaying ] = useState < string > ( '' ) ;
254269 const [ list , setList ] = useState < ListItem [ ] > ( [ ] ) ;
255270 const [ nbOfRecording , setNumberOfRecording ] = useState < number > ( 0 ) ;
256271 const [ currentPlaybackSpeed , setCurrentPlaybackSpeed ] =
@@ -350,8 +365,6 @@ const AppContainer = () => {
350365 { list . map ( item => (
351366 < RenderListItem
352367 key = { item . path }
353- currentPlaying = { currentPlaying }
354- setCurrentPlaying = { setCurrentPlaying }
355368 item = { item }
356369 onPanStateChange = { value => setShouldScroll ( ! value ) }
357370 { ...{ currentPlaybackSpeed, changeSpeed } }
0 commit comments