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
|
import * as types from 'app/types'
import { store, history, dispatch } from 'app/store'
import { URLS } from 'app/constants'
const audioPlayer = document.createElement('audio')
// audioPlayer.volume = 0.0
audioPlayer.addEventListener('play', () => {
dispatch({ type: types.audio.play })
})
audioPlayer.addEventListener('pause', () => {
dispatch({ type: types.audio.pause })
})
audioPlayer.addEventListener('timeupdate', () => {
dispatch({ type: types.audio.update_time, play_ts: audioPlayer.currentTime })
})
export const load = () => dispatch => {
return new Promise((resolve, reject) => {
audioPlayer.addEventListener('loadedmetadata', () => {
// console.log('audio duration:', audioPlayer.duration)
dispatch({ type: types.align.set_display_setting, key: 'duration', value: audioPlayer.duration })
resolve()
})
audioPlayer.src = URLS.audio
})
}
export const play = () => dispatch => {
dispatch({ type: types.audio.play })
audioPlayer.play()
}
export const pause = () => dispatch => {
dispatch({ type: types.audio.pause })
audioPlayer.pause()
}
export const seek = play_ts => dispatch => {
audioPlayer.currentTime = play_ts
dispatch({ type: types.audio.seek, seek_ts: audioPlayer.currentTime })
}
export const jump = delta_ts => dispatch => {
audioPlayer.currentTime += delta_ts
dispatch({ type: types.audio.seek, seek_ts: audioPlayer.currentTime })
}
export const toggle = () => dispatch => {
if (store.getState().audio.playing) {
pause()(dispatch)
} else {
play()(dispatch)
}
}
export const setVolume = volume => dispatch => {
audioPlayer.volume = volume
dispatch({ type: types.audio.set_volume, volume })
}
export const setCC = value => dispatch => {
dispatch({ type: types.audio.set_cc, value })
}
|