-
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathAudioBar.svelte
More file actions
235 lines (224 loc) · 7.61 KB
/
AudioBar.svelte
File metadata and controls
235 lines (224 loc) · 7.61 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!--
@component
Based on an audio component found at https://svelte.dev/repl/b0a901d9a15347bd95466150485e4a78?version=3.31.0.
Wraps a JS-created HTMLAudioElement with a basic UI with a progress bar and Play/Pause, Seek, and Skip functionality.
TODO:
- display audio not found message in UI when audio is not found
-->
<script lang="ts">
import config from '$assets/config';
import {
changeVerse,
format,
playPause,
seek,
seekOffset,
skip,
updatePlaybackSpeed
} from '$lib/data/audio';
import {
audioPlayer,
convertStyle,
modal,
ModalType,
playMode,
refs,
s,
t,
userSettings,
type PlayModeSettings
} from '$lib/data/stores';
import { AudioIcon } from '$lib/icons';
import PlayButton from './PlayButton.svelte';
import RepeatButton from './RepeatButton.svelte';
function mayResetPlayMode(hasTiming: boolean) {
// If the current mode is repeatSelection and the reference is changed to something without timing
// (even chapter without audio), then reset the playMode. This matches how the Android app behaves.
if (!hasTiming && $playMode.mode === 'repeatSelection') {
playMode.reset();
}
}
function seekAudio(event: MouseEvent) {
if (!$audioPlayer.loaded) {
return;
}
const progressBar = document.getElementById('progress-bar');
const percent =
(event.clientX - (progressBar?.offsetLeft ?? 0)) / (progressBar?.offsetWidth ?? 1);
// Set the current time of the audio element to the corresponding time based on the percent
seek($audioPlayer.duration * percent);
}
let lastPlayMode = '';
function playModeChanged(value: PlayModeSettings) {
let key = '';
switch (value.mode) {
case 'stop':
key = 'Audio_Repeat_Off_Stop';
break;
case 'repeatPage':
key = 'Audio_Repeat_Page';
break;
case 'repeatSelection':
key = 'Audio_Repeat_Selection';
break;
case 'continue':
key = 'Audio_Repeat_Off_Continue';
break;
}
if (lastPlayMode !== '' && lastPlayMode !== value.mode) {
startShowHint($t[key]);
}
lastPlayMode = value.mode;
}
$effect(() => playModeChanged($playMode));
let hintText = $state('');
let showHint = $state(false);
let hintTimeoutId: NodeJS.Timeout | null = null;
function startShowHint(text: string) {
showHint = true;
hintText = text;
if (hintTimeoutId) {
clearTimeout(hintTimeoutId);
}
hintTimeoutId = setTimeout(() => {
showHint = false;
hintTimeoutId = null;
}, 1500);
}
const showSpeed = config.mainFeatures['settings-audio-speed'];
const showRepeatMode = config.mainFeatures['audio-repeat-mode-button'];
const hintStyle = convertStyle($s?.['ui.bar.audio.hint.text']);
const playButtonState = $derived($audioPlayer.playing ? 'pause' : 'play');
const iconColor = $derived($s?.['ui.bar.audio.icon']['color']);
const iconPlayColor = $derived($s?.['ui.bar.audio.play.icon']['color']);
const backgroundColor = $derived($s?.['ui.bar.audio']['background-color']);
const audioBarClass = $derived($refs.hasAudio?.timingFile ? 'audio-bar' : 'audio-bar-progress');
$effect(() => mayResetPlayMode($refs.hasAudio?.timing));
$effect(() => updatePlaybackSpeed($userSettings['audio-speed'] as string));
</script>
<div class="relative {audioBarClass}" style:background-color={backgroundColor}>
{#if showHint}
<div
style={hintStyle}
class="absolute flex flex-row justify-center -top-[3rem] p-2 w-full left-1/2 -translate-x-1/2 max-w-screen-md shadow-md"
>
{hintText}
</div>
{/if}
{#if showRepeatMode}
<div class="audio-control-buttons">
<RepeatButton
color="{iconColor},"
onclick={() => playMode.next($refs.hasAudio?.timingFile)}
state={$playMode.mode}
/>
</div>
{/if}
<!-- Play Controls -->
<div class="audio-controls" style:direction="ltr">
<button class="audio-control-buttons" onclick={() => skip(-1)}>
<AudioIcon.SkipPrevious color={iconColor} />
</button>
{#if $refs.hasAudio?.timingFile}
<button class="audio-control-buttons" onclick={() => changeVerse(-1)}>
<AudioIcon.Rewind color={iconColor} />
</button>
{:else}
<button class="audio-control-buttons" onclick={() => seekOffset(-10)}>
<AudioIcon.Replay10 color={iconColor} />
</button>
{/if}
<PlayButton state={playButtonState} color={iconPlayColor} onclick={playPause} />
{#if $refs.hasAudio?.timingFile}
<button class="audio-control-buttons" onclick={() => changeVerse(1)}>
<AudioIcon.FastForward color={iconColor} />
</button>
{:else}
<button class="audio-control-buttons" onclick={() => seekOffset(10)}>
<AudioIcon.Forward10 color={iconColor} />
</button>
{/if}
<button class="audio-control-buttons" onclick={() => skip(1)}>
<AudioIcon.SkipNext color={iconColor} />
</button>
</div>
{#if showSpeed}
<button
class="audio-speed audio-control-buttons"
onclick={() => modal.open(ModalType.PlaybackSpeed)}
>
<AudioIcon.Speed color={iconColor} />
</button>
{/if}
{#if !$refs.hasAudio.timingFile}
<!-- Progress Bar -->
<div class="audio-progress-value text-sm">
{format($audioPlayer.progress)}
</div>
{#if $audioPlayer.loaded}
<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
<progress
id="progress-bar"
class="dy-progress audio-progress"
value={$audioPlayer.progress}
max={$audioPlayer.duration}
onclick={seekAudio}
></progress>
{:else}
<progress class="dy-progress audio-progress" value="0" max="1"></progress>
{/if}
<div class="audio-progress-duration text-sm">
{format($audioPlayer.duration)}
</div>
{/if}
</div>
<style>
.audio-bar {
display: grid;
grid-auto-columns: 3.125rem auto 3.125rem;
grid-auto-rows: 4rem;
}
.audio-bar-progress {
display: grid;
grid-auto-columns: 3.125rem auto 3.125rem;
grid-auto-rows: 3.125rem 1.875rem;
}
.audio-progress-value {
grid-row: 2;
grid-column: 1;
place-self: center;
}
.audio-progress-duration {
grid-row: 2;
grid-column: 3;
place-self: center;
}
.audio-progress {
grid-row: 2;
grid-column: 2;
place-self: center;
}
.audio-repeat {
grid-row: 1;
grid-column: 1;
place-self: center;
}
.audio-controls {
display: inline-flex;
grid-row: 1;
grid-column: 2;
place-self: center;
align-items: center;
}
.audio-control-buttons {
margin-inline-start: 12px;
margin-inline-end: 12px;
place-self: center;
}
.audio-speed {
grid-row: 1;
grid-column: 3;
place-self: center;
}
</style>