@@ -10,6 +10,7 @@ import {
1010 PanResponder ,
1111 ScrollView ,
1212 View ,
13+ type LayoutChangeEvent ,
1314 type LayoutRectangle ,
1415 type NativeTouchEvent ,
1516} from 'react-native' ;
@@ -114,7 +115,9 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
114115 durationType : DurationType . max ,
115116 } ) ;
116117 if ( ! isNil ( duration ) ) {
117- setSongDuration ( duration ) ;
118+ const audioDuration = Number ( duration ) ;
119+ setSongDuration ( audioDuration > 0 ? audioDuration : 0 ) ;
120+ return Promise . resolve ( audioDuration ) ;
118121 } else {
119122 return Promise . reject (
120123 new Error ( `Could not get duration for path: ${ path } ` )
@@ -129,7 +132,10 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
129132 try {
130133 const prepare = await preparePlayerForPath ( ) ;
131134 if ( prepare ) {
132- await getAudioDuration ( ) ;
135+ const duration = await getAudioDuration ( ) ;
136+ if ( duration < 0 ) {
137+ await getAudioDuration ( ) ;
138+ }
133139 }
134140 } catch ( err ) {
135141 console . error ( err ) ;
@@ -174,7 +180,6 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
174180 const result = await stopPlayer ( {
175181 playerKey : `PlayerFor${ path } ` ,
176182 } ) ;
177- await preparePlayerForPath ( ) ;
178183 if ( ! isNil ( result ) && result ) {
179184 setCurrentProgress ( 0 ) ;
180185 setPlayerState ( PlayerState . stopped ) ;
@@ -197,6 +202,10 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
197202 const startPlayerAction = async ( args ?: IStartPlayerRef ) => {
198203 if ( mode === 'static' ) {
199204 try {
205+ if ( playerState === PlayerState . stopped ) {
206+ await preparePlayerForPath ( ) ;
207+ }
208+
200209 const play = await playPlayer ( {
201210 finishMode : FinishMode . stop ,
202211 playerKey : `PlayerFor${ path } ` ,
@@ -409,7 +418,13 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
409418
410419 const tracePlaybackValue = onCurrentDuration ( data => {
411420 if ( data . playerKey === `PlayerFor${ path } ` ) {
412- setCurrentProgress ( data . currentDuration ) ;
421+ const currentAudioDuration = Number ( data . currentDuration ) ;
422+
423+ if ( ! isNaN ( currentAudioDuration ) ) {
424+ setCurrentProgress ( currentAudioDuration ) ;
425+ } else {
426+ setCurrentProgress ( 0 ) ;
427+ }
413428 }
414429 } ) ;
415430
@@ -500,18 +515,17 @@ export const Waveform = forwardRef<IWaveformRef, IWaveform>((props, ref) => {
500515 < View
501516 ref = { viewRef }
502517 style = { styles . waveformInnerContainer }
503- onLayout = { ( ) => {
504- viewRef . current ?. measure ( ( _x , _y , width , height , pageX , pageY ) => {
505- setViewLayout ( { height, width, x : pageX , y : pageY } ) ;
506- } ) ;
518+ onLayout = { ( event : LayoutChangeEvent ) => {
519+ const { height, width, x, y } = event . nativeEvent . layout ;
520+ setViewLayout ( { height, width, x, y } ) ;
507521 } }
508522 { ...( mode === 'static' ? panResponder . panHandlers : { } ) } >
509523 < ScrollView
510524 horizontal
511525 ref = { scrollRef }
512526 style = { styles . scrollContainer }
513527 scrollEnabled = { mode === 'live' } >
514- { waveform . map ( ( amplitude , indexCandle ) => (
528+ { waveform ? .map ?. ( ( amplitude , indexCandle ) => (
515529 < WaveformCandle
516530 key = { indexCandle }
517531 index = { indexCandle }
0 commit comments