Skip to main content

Bạn có thể bind thuộc tính của phần tử <audio><video>, giúp bạn dễ dàng xây dựng giao diện trình phát tùy chỉnh, chẳng hạn như AudioPlayer.svelte.

Đầu tiên, thêm phần tử <audio> cùng với các phép bind của nó (chúng ta sẽ sử dụng dạng viết tắt cho src, durationpaused):

AudioPlayer.svelte
<div class="player" class:paused>
	<audio
		{src}
		bind:currentTime={time}
		bind:duration
		bind:paused
	></audio>

	<button
		class="play"
		aria-label={paused ? 'play' : 'pause'}
	></button>

Tiếp theo, thêm một trình xử lý sự kiện cho

Tiếp theo: Kích thước

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<script>
	import AudioPlayer from './AudioPlayer.svelte';
	import { tracks } from './tracks.js';
</script>
 
<div class="centered">
	{#each tracks as track}
		<AudioPlayer {...track} />
	{/each}
</div>
 
<style>
	.centered {
		display: flex;
		flex-direction: column;
		height: 100%;
		justify-content: center;
		gap: 0.5em;
		max-width: 40em;
		margin: 0 auto;
	}
</style>
initialising