-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerAPIAudio.tsx
More file actions
108 lines (93 loc) · 3.94 KB
/
Copy pathPlayerAPIAudio.tsx
File metadata and controls
108 lines (93 loc) · 3.94 KB
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
import OmniAural, { useOmniAural } from 'omniaural'
import { createRef, useEffect } from 'react'
import { unstable_batchedUpdates } from 'react-dom'
import { retrieveLatestChaptersForEpisodeId } from '~/services/mediaRef'
import { playerGetDuration, playerUpdateDuration, playerUpdatePlaybackPosition } from '~/services/player/player'
import { audioInitialize, audioSeekTo, audioGetPosition } from '~/services/player/playerAudio'
import { enrichChapterDataForPlayer, handleChapterUpdateInterval } from '~/services/player/playerChapters'
import { generateChapterFlagPositions, setClipFlagPositions } from '~/services/player/playerFlags'
import { handlePlayerTimeUpdate } from '~/services/player/playerAdSkip'
import { addOrUpdateHistoryItemOnServer } from '~/services/userHistoryItem'
import { OmniAuralState } from '~/state/omniauralState'
// TODO: temporarily using require instead of require to work around a build error happening
// in the Github action pipeline: "'PlayerAudio' cannot be used as a JSX component."
// eslint-disable-next-line @typescript-eslint/no-var-requires
const PlayerAudio = require('react-h5-audio-player').default
type Props = unknown
export const PlayerAPIAudio = (props: Props) => {
const [player] = useOmniAural('player') as [OmniAuralState['player']]
// const [historyItemsIndex] = useOmniAural('historyItemsIndex')
const historyItemsIndex = OmniAural.state.historyItemsIndex.value()
const { currentNowPlayingItem } = player
useEffect(() => {
audioInitialize()
}, [])
/* Never initialize PlayerAPIs on the server-side. */
if (typeof window === 'undefined') {
return null
}
const _onEnded = () => {
addOrUpdateHistoryItemOnServer({
nowPlayingItem: currentNowPlayingItem,
playbackPosition: 0,
mediaFileDuration: playerGetDuration(),
forceUpdateOrderDate: false,
skipSetNowPlaying: true,
completed: true
})
}
const _onLoadedMetaData = async () => {
if (currentNowPlayingItem.clipStartTime) {
audioSeekTo(currentNowPlayingItem.clipStartTime)
}
const duration = playerGetDuration()
const historyItem = historyItemsIndex.episodes[currentNowPlayingItem.episodeId]
if (Number.isInteger(currentNowPlayingItem.clipStartTime)) {
setClipFlagPositions(currentNowPlayingItem, duration)
} else if (historyItem && !currentNowPlayingItem.liveItem) {
audioSeekTo(historyItem.p)
}
const playbackPosition = currentNowPlayingItem.clipStartTime || historyItem?.p || 0
addOrUpdateHistoryItemOnServer({
nowPlayingItem: currentNowPlayingItem,
playbackPosition,
mediaFileDuration: duration,
forceUpdateOrderDate: true,
skipSetNowPlaying: false
})
if (currentNowPlayingItem.episodeChaptersUrl) {
const data = await retrieveLatestChaptersForEpisodeId(currentNowPlayingItem.episodeId)
const [chapters] = data
const enrichedChapters = enrichChapterDataForPlayer(chapters, duration)
const chapterFlagPositions = generateChapterFlagPositions(enrichedChapters, duration)
OmniAural.setChapterFlagPositions(chapterFlagPositions)
OmniAural.setChapters(enrichedChapters)
}
}
const _onListen = () => {
unstable_batchedUpdates(() => {
playerUpdatePlaybackPosition()
playerUpdateDuration()
handleChapterUpdateInterval()
// Ad skip logic: check if current playback position is in an ad segment
const currentTime = audioGetPosition()
handlePlayerTimeUpdate(currentTime, audioSeekTo)
})
}
window.playerAudio = createRef()
return (
<PlayerAudio
/*
NOTE: I had to set preload to metadata to avoid bugs with WebViews
refusing to handle changes to the <audio> currentTime properly.
Apparently using preload auto in <audio> causes bugs in WebViews.
*/
preload='metadata'
onEnded={_onEnded}
onLoadedMetaData={_onLoadedMetaData}
onListen={_onListen}
onSeeked={_onListen}
ref={window.playerAudio}
/>
)
}