/
audio-player.tsx
158 lines (150 loc) · 4.29 KB
/
audio-player.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react'
import * as Kb from '../../common-adapters'
import * as Styles from '../../styles'
import AudioVideo from './audio-video'
import {formatAudioRecordDuration} from '../../util/timestamp'
type VisProps = {
amps: Array<number>
ampsRemain: number
height: number
maxWidth?: number
}
const ampHeightProp = (amp: number) => {
return Math.max(0.05, Math.min(1.0, 1 - amp / -50))
}
const AudioVis = (props: VisProps) => {
let maxHeight = 0
const content = props.amps.map((amp, index) => {
const heightProp = ampHeightProp(amp)
const height = heightProp * props.height
if (height >= maxHeight) {
maxHeight = height
}
return (
<Kb.Box2
alignSelf="flex-end"
direction="vertical"
key={index}
style={{
backgroundColor: index < props.ampsRemain ? Styles.globalColors.blue : Styles.globalColors.black,
height,
marginRight: 2,
width: 1,
}}
/>
)
})
return Styles.isMobile ? (
<Kb.ScrollView
horizontal={true}
style={{height: maxHeight, maxWidth: props.maxWidth}}
showsHorizontalScrollIndicator={false}
>
{content}
</Kb.ScrollView>
) : (
<Kb.Box2
direction="horizontal"
style={{height: maxHeight, marginTop: Styles.globalMargins.xtiny, maxWidth: props.maxWidth}}
>
{content}
</Kb.Box2>
)
}
type Props = {
duration: number
maxWidth?: number
url: string
visAmps: Array<number>
}
const AudioPlayer = (props: Props) => {
const vidRef = React.useRef<AudioVideo>(null)
const [timeStart, setTimeStart] = React.useState(0)
const [timeLeft, setTimeLeft] = React.useState(props.duration)
const [timePaused, setTimePaused] = React.useState(0)
const [timePauseButton, setTimePauseButton] = React.useState(0)
const [paused, setPaused] = React.useState(true)
const onClick = () => {
if (paused) {
if (timeStart === 0) {
setTimeStart(Date.now())
} else if (timePauseButton > 0) {
setTimePaused(timePaused + (Date.now() - timePauseButton))
}
setPaused(false)
setTimePauseButton(0)
} else {
setTimePauseButton(Date.now())
setPaused(true)
}
}
React.useEffect(() => {
if (paused) {
return
}
const timer = setTimeout(() => {
const diff = Date.now() - timeStart - timePaused
const newTimeLeft = props.duration - diff
if (newTimeLeft <= 0) {
setTimeLeft(props.duration)
setPaused(true)
setTimeStart(0)
setTimePaused(0)
setTimePauseButton(0)
if (vidRef.current) {
vidRef.current.seek(0)
}
} else {
setTimeLeft(newTimeLeft)
}
}, 100)
return () => {
clearTimeout(timer)
}
}, [timeLeft, timeStart, timePaused, paused, props.duration])
const ampsRemain = Math.floor((1 - timeLeft / props.duration) * props.visAmps.length)
return (
<Kb.Box2 direction="horizontal" style={styles.container} gap="tiny">
<Kb.ClickableBox onClick={props.url ? onClick : undefined} style={{justifyContent: 'center'}}>
<Kb.Icon
type={!paused ? 'iconfont-pause' : 'iconfont-play'}
fontSize={32}
style={Kb.iconCastPlatformStyles(styles.play)}
/>
</Kb.ClickableBox>
<Kb.Box2 direction="vertical" style={styles.visContainer} gap="xxtiny">
<AudioVis height={32} amps={props.visAmps} maxWidth={props.maxWidth} ampsRemain={ampsRemain} />
<Kb.Text type="BodyTiny">{formatAudioRecordDuration(timeLeft)}</Kb.Text>
</Kb.Box2>
{props.url.length > 0 && <AudioVideo ref={vidRef} url={props.url} paused={paused} />}
</Kb.Box2>
)
}
const styles = Styles.styleSheetCreate(() => ({
button: {
borderRadius: 15,
height: 30,
position: 'relative',
width: 30,
},
container: {
...Styles.padding(Styles.globalMargins.xxtiny, Styles.globalMargins.tiny),
backgroundColor: Styles.globalColors.white,
borderColor: Styles.globalColors.grey,
borderRadius: Styles.borderRadius,
borderStyle: 'solid',
borderWidth: 1,
marginTop: Styles.globalMargins.xtiny,
},
play: {
color: Styles.globalColors.blue,
},
vis: {
alignSelf: 'flex-start',
},
visContainer: {
alignItems: 'flex-start',
minWidth: 40,
},
}))
export default AudioPlayer