@@ -12,6 +12,7 @@ import commonStyles from '../styles/commonStyles';
1212import RNSlider from './RNSlider' ;
1313import style from './styles' ;
1414import { useFocusEffect } from '@react-navigation/native' ;
15+ import PropTypes from 'prop-types' ;
1516
1617const PLAY_STATE_PAUSED = 'paused' ;
1718const PLAY_STATE_PLAYING = 'playing' ;
@@ -22,6 +23,15 @@ export const pauseAudio = () => {
2223 soundRef ?. current ?. updateState ?. ( ) ;
2324} ;
2425
26+ const propTypes = {
27+ mediaStatus : PropTypes . string ,
28+ uri : PropTypes . string ,
29+ msgId : PropTypes . string ,
30+ media : PropTypes . shape ( {
31+ duration : PropTypes . number , // Ensures media.duration is a number
32+ } ) ,
33+ } ;
34+
2535const AudioPlayer = props => {
2636 const { media, msgId, uri, mediaStatus } = props ;
2737 const [ playState , setPlayState ] = React . useState ( PLAY_STATE_PAUSED ) ;
@@ -41,12 +51,7 @@ const AudioPlayer = props => {
4151
4252 React . useEffect ( ( ) => {
4353 let timeout = setInterval ( ( ) => {
44- if (
45- sound . current &&
46- sound ?. current ?. isLoaded ( ) &&
47- playStateRef . current === PLAY_STATE_PLAYING &&
48- ! sliderEditing . current
49- ) {
54+ if ( sound ?. current ?. isLoaded ( ) && playStateRef . current === PLAY_STATE_PLAYING && ! sliderEditing . current ) { //NOSONAR
5055 sound . current . getCurrentTime ( seconds => {
5156 setPlaySeconds ( seconds ) ;
5257 } ) ;
@@ -72,40 +77,49 @@ const AudioPlayer = props => {
7277 } , [ appState ] ) ;
7378
7479 React . useEffect ( ( ) => {
75- if ( sound . current && playState === PLAY_STATE_PLAYING ) {
76- if ( soundRef . current && soundRef . current . msgId !== msgId ) {
77- soundRef . current . pause ( ) ;
78- soundRef ?. current ?. updateState ?. ( ) ;
79- }
80+ const initializeSound = filepath => {
81+ Sound . setCategory ( 'Playback' ) ;
82+ sound . current = new Sound ( filepath , '' , error => {
83+ if ( error ) {
84+ console . log ( error , 'Play Error' ) ;
85+ setPlayState ( PLAY_STATE_PAUSED ) ;
86+ } else {
87+ handleSoundLoaded ( ) ;
88+ }
89+ } ) ;
90+ } ;
91+
92+ const handleSoundLoaded = ( ) => {
93+ setPlayState ( PLAY_STATE_PLAYING ) ;
94+ playStateRef . current = PLAY_STATE_PLAYING ;
95+ setTimeout ( ( ) => {
96+ configureSoundRef ( ) ;
97+ sound . current . setCurrentTime ( playSeconds ) ;
98+ sound . current . play ( playComplete ) ;
99+ } ) ;
100+ } ;
101+
102+ const configureSoundRef = ( ) => {
80103 soundRef . current = sound . current ;
81104 soundRef . current . msgId = msgId ;
82105 soundRef . current . updateState = ( ) => {
83106 setPlayState ( PLAY_STATE_PAUSED ) ;
84107 } ;
85- sound . current . play ( playComplete ) ;
86- } else {
87- const filepath = uri ;
88- if ( playState === PLAY_STATE_LOADING && filepath ) {
89- Sound . setCategory ( 'Playback' ) ;
90- sound . current = new Sound ( filepath , '' , error => {
91- if ( error ) {
92- console . log ( error , 'Play Error' ) ;
93- setPlayState ( PLAY_STATE_PAUSED ) ;
94- } else {
95- setPlayState ( PLAY_STATE_PLAYING ) ;
96- playStateRef . current = PLAY_STATE_PLAYING ;
97- setTimeout ( ( ) => {
98- soundRef . current = sound . current ;
99- soundRef . current . msgId = msgId ;
100- soundRef . current . updateState = ( ) => {
101- setPlayState ( PLAY_STATE_PAUSED ) ;
102- } ;
103- sound . current . setCurrentTime ( playSeconds ) ;
104- sound . current . play ( playComplete ) ;
105- } ) ;
106- }
107- } ) ;
108+ } ;
109+
110+ const handlePlayStatePlaying = ( ) => {
111+ if ( soundRef . current && soundRef . current . msgId !== msgId ) {
112+ soundRef . current . pause ( ) ;
113+ soundRef ?. current ?. updateState ?. ( ) ;
108114 }
115+ configureSoundRef ( ) ;
116+ sound . current . play ( playComplete ) ;
117+ } ;
118+
119+ if ( sound . current && playState === PLAY_STATE_PLAYING ) {
120+ handlePlayStatePlaying ( ) ;
121+ } else if ( playState === PLAY_STATE_LOADING && uri ) {
122+ initializeSound ( uri ) ;
109123 }
110124 } , [ playState ] ) ;
111125
@@ -144,12 +158,22 @@ const AudioPlayer = props => {
144158 setPlayState ( PLAY_STATE_PAUSED ) ;
145159 } ;
146160
147- const getAudioTimeString = seconds => {
148- let h = Math . floor ( seconds / 3600 ) ;
149- let m = Math . floor ( ( seconds % 3600 ) / 60 ) ;
150- let s = Math . floor ( seconds % 60 ) ;
161+ const getAudioTimeString = totalSeconds => {
162+ let h = Math . floor ( totalSeconds / 3600 ) ;
163+ let m = Math . floor ( ( totalSeconds % 3600 ) / 60 ) ;
164+ let s = Math . floor ( totalSeconds % 60 ) ;
165+
166+ let hours = '' ;
167+ if ( h > 0 ) {
168+ hours = h < 10 ? `0${ h } ` : h ;
169+ hours += ':' ;
170+ }
171+ let minutes = m < 10 ? `0${ m } ` : m ;
172+ let seconds = s < 10 ? `0${ s } ` : s ;
173+
174+ minutes += ':' ;
151175
152- return ( h > 0 ? ( h < 10 ? `0 ${ h } ` : h ) + ':' : '' ) + ( m < 10 ? `0 ${ m } ` : m ) + ':' + ( s < 10 ? `0 ${ s } ` : s ) ;
176+ return hours + minutes + seconds ;
153177 } ;
154178
155179 const playComplete = success => {
@@ -215,4 +239,6 @@ const AudioPlayer = props => {
215239 ) ;
216240} ;
217241
242+ AudioPlayer . propTypes = propTypes ;
243+
218244export default AudioPlayer ;
0 commit comments