@@ -7,18 +7,24 @@ import {isMobile} from '../../constants/platform'
77
88type VisProps = {
99 amps : Array < number >
10- ampsRemain : number
10+ playedRatio : number
1111 height : number
1212 maxWidth ?: number
1313}
1414
1515const AudioVis = ( props : VisProps ) => {
16+ const { amps, playedRatio, maxWidth} = props
17+ let threshold = Math . floor ( amps . length * playedRatio )
18+ if ( threshold > 0 ) {
19+ threshold += 2 // metering actually lags so compensate while actually playing
20+ }
21+
1622 let maxHeight = 0
17- const content = props . amps . map ( ( inamp , index ) => {
23+ const content = amps . map ( ( inamp , index ) => {
1824 const amp = isNaN ( inamp ) || inamp < 0 ? 0 : inamp
1925 const prop = Math . min ( 1.0 , Math . max ( Math . sqrt ( amp ) , 0.05 ) )
2026 const height = Math . floor ( prop * props . height )
21- if ( height >= maxHeight ) {
27+ if ( height > maxHeight ) {
2228 maxHeight = height
2329 }
2430 return (
@@ -27,7 +33,7 @@ const AudioVis = (props: VisProps) => {
2733 direction = "vertical"
2834 key = { index }
2935 style = { {
30- backgroundColor : index < props . ampsRemain ? Styles . globalColors . blue : Styles . globalColors . black ,
36+ backgroundColor : index < threshold ? Styles . globalColors . blue : Styles . globalColors . black ,
3137 height,
3238 marginRight : isMobile ? 4 * Styles . hairlineWidth : 2 ,
3339 width : isMobile ? 3 * Styles . hairlineWidth : 1 ,
@@ -38,15 +44,15 @@ const AudioVis = (props: VisProps) => {
3844 return Styles . isMobile ? (
3945 < Kb . ScrollView
4046 horizontal = { true }
41- style = { { height : maxHeight , maxWidth : props . maxWidth } }
47+ style = { { height : maxHeight , maxWidth : maxWidth } }
4248 showsHorizontalScrollIndicator = { false }
4349 >
4450 { content }
4551 </ Kb . ScrollView >
4652 ) : (
4753 < Kb . Box2
4854 direction = "horizontal"
49- style = { { height : maxHeight , marginTop : Styles . globalMargins . xtiny , maxWidth : props . maxWidth } }
55+ style = { { height : maxHeight , marginTop : Styles . globalMargins . xtiny , maxWidth : maxWidth } }
5056 >
5157 { content }
5258 </ Kb . Box2 >
@@ -62,73 +68,57 @@ type Props = {
6268}
6369
6470const AudioPlayer = ( props : Props ) => {
71+ const { duration, big, maxWidth, url, visAmps} = props
6572 const seekRef = React . useRef < null | ( ( n : number ) => void ) > ( null )
66- const [ timeStart , setTimeStart ] = React . useState ( 0 )
67- const [ timeLeft , setTimeLeft ] = React . useState ( props . duration )
68- const [ timePaused , setTimePaused ] = React . useState ( 0 )
69- const [ timePauseButton , setTimePauseButton ] = React . useState ( 0 )
73+ const [ playedRatio , setPlayedRatio ] = React . useState ( 0 )
7074 const [ paused , setPaused ] = React . useState ( true )
7175 const onClick = ( ) => {
7276 if ( paused ) {
73- if ( timeStart === 0 ) {
74- setTimeStart ( Date . now ( ) )
75- } else if ( timePauseButton > 0 ) {
76- setTimePaused ( timePaused + ( Date . now ( ) - timePauseButton ) )
77- }
7877 setPaused ( false )
79- setTimePauseButton ( 0 )
8078 } else {
81- setTimePauseButton ( Date . now ( ) )
8279 setPaused ( true )
8380 }
8481 }
85- React . useEffect ( ( ) => {
86- if ( paused ) {
87- return
88- }
89- const timer = setTimeout ( ( ) => {
90- const diff = Date . now ( ) - timeStart - timePaused
91- const newTimeLeft = props . duration - diff
92- if ( newTimeLeft <= 0 ) {
93- setTimeLeft ( props . duration )
94- setPaused ( true )
95- setTimeStart ( 0 )
96- setTimePaused ( 0 )
97- setTimePauseButton ( 0 )
98- seekRef . current ?.( 0 )
99- } else {
100- setTimeLeft ( newTimeLeft )
101- }
102- } , 100 )
103- return ( ) => {
104- clearTimeout ( timer )
105- }
106- } , [ timeLeft , timeStart , timePaused , paused , props . duration ] )
10782
108- const ampsRemain = Math . floor ( ( 1 - timeLeft / props . duration ) * props . visAmps . length )
83+ const onPositionUpdated = React . useCallback (
84+ ( ratio : number ) => {
85+ setPlayedRatio ( ratio )
86+ } ,
87+ [ setPlayedRatio ]
88+ )
89+
90+ const onEnded = React . useCallback ( ( ) => {
91+ setPaused ( true )
92+ setPlayedRatio ( 0 )
93+ } , [ setPaused , setPlayedRatio ] )
94+
95+ const timeLeft = duration - playedRatio * duration
10996 return (
11097 < Kb . Box2
11198 direction = "horizontal"
112- style = { Styles . collapseStyles ( [ styles . container , { height : props . big ? 56 : 40 } ] ) }
99+ style = { Styles . collapseStyles ( [ styles . container , { height : big ? 56 : 40 } ] ) }
113100 gap = "tiny"
114101 >
115- < Kb . ClickableBox onClick = { props . url ? onClick : undefined } style = { { justifyContent : 'center' } } >
102+ < Kb . ClickableBox onClick = { url ? onClick : undefined } style = { { justifyContent : 'center' } } >
116103 < Kb . Icon
117104 type = { ! paused ? 'iconfont-pause' : 'iconfont-play' }
118105 fontSize = { 32 }
119- color = { props . url ? Styles . globalColors . blue : Styles . globalColors . grey }
106+ color = { url ? Styles . globalColors . blue : Styles . globalColors . grey }
120107 />
121108 </ Kb . ClickableBox >
122109 < Kb . Box2 direction = "vertical" style = { styles . visContainer } gap = "xxtiny" fullHeight = { true } >
123- < AudioVis
124- height = { props . big ? 32 : 18 }
125- amps = { props . visAmps }
126- maxWidth = { props . maxWidth }
127- ampsRemain = { ampsRemain }
128- />
110+ < AudioVis height = { big ? 32 : 18 } amps = { visAmps } maxWidth = { maxWidth } playedRatio = { playedRatio } />
129111 < Kb . Text type = "BodyTiny" > { formatAudioRecordDuration ( timeLeft ) } </ Kb . Text >
130112 </ Kb . Box2 >
131- { props . url . length > 0 && < AudioVideo seekRef = { seekRef } url = { props . url } paused = { paused } /> }
113+ { url . length > 0 && (
114+ < AudioVideo
115+ seekRef = { seekRef }
116+ url = { url }
117+ paused = { paused }
118+ onPositionUpdated = { onPositionUpdated }
119+ onEnded = { onEnded }
120+ />
121+ ) }
132122 </ Kb . Box2 >
133123 )
134124}
0 commit comments