|
1 | 1 | <script context="module" lang="ts"> |
2 | 2 | import throttle from 'lodash.throttle'; |
3 | | - import { SoundCloud } from '../lib/soundcloud'; |
4 | | - import type { PlaybackProgress } from '../models/PlaybackProgress'; |
5 | 3 | import { player } from '../stores/player'; |
6 | 4 | import { clamp } from '../utils/clamp'; |
7 | 5 |
|
8 | 6 | let audio = new Audio(); |
9 | 7 | (audio as any).mozAudioChannelType = 'content'; |
10 | 8 |
|
11 | 9 | audio.onloadedmetadata = () => { |
12 | | - updateStatus({ duration: audio.duration }); |
| 10 | + player.update({ duration: audio.duration }); |
13 | 11 | }; |
14 | 12 |
|
15 | 13 | audio.ontimeupdate = throttle(() => { |
16 | | - updateStatus({ currentTime: audio.currentTime }); |
| 14 | + player.update({ currentTime: audio.currentTime }); |
17 | 15 | }, 1000); |
18 | 16 |
|
19 | | - export async function load(trackId: number) { |
20 | | - const [track, streamUrl] = await Promise.all([ |
21 | | - new SoundCloud().track.get(trackId), |
22 | | - new SoundCloud().track.getStreamUrl(trackId), |
23 | | - ]); |
| 17 | + audio.onended = async () => { |
| 18 | + nextTrack(); |
| 19 | + }; |
| 20 | +
|
| 21 | + export async function loadTrack(trackId: number) { |
| 22 | + const streamUrl = await player.loadTrack(trackId); |
| 23 | +
|
| 24 | + audio.src = streamUrl; |
| 25 | + audio.currentTime = 0; |
| 26 | +
|
| 27 | + audio.play(); |
| 28 | + player.update({ playing: true }); |
| 29 | + } |
| 30 | +
|
| 31 | + export async function loadPlaylist(playlistId: number) { |
| 32 | + const streamUrl = await player.loadPlaylist(playlistId); |
| 33 | +
|
| 34 | + audio.src = streamUrl; |
| 35 | + audio.currentTime = 0; |
| 36 | +
|
| 37 | + audio.play(); |
| 38 | + player.update({ playing: true }); |
| 39 | + } |
| 40 | +
|
| 41 | + export async function nextTrack() { |
| 42 | + const streamUrl = await player.nextTrack(); |
| 43 | + if (!streamUrl) return; |
24 | 44 |
|
25 | 45 | audio.src = streamUrl; |
26 | 46 | audio.currentTime = 0; |
27 | 47 |
|
28 | | - player.set({ |
29 | | - track, |
30 | | - currentTime: 0, |
31 | | - duration: track.duration || 0, |
32 | | - playing: true, |
33 | | - }); |
| 48 | + audio.play(); |
| 49 | + player.update({ playing: true }); |
| 50 | + } |
| 51 | +
|
| 52 | + export async function previousTrack() { |
| 53 | + const streamUrl = await player.previousTrack(); |
| 54 | + if (!streamUrl) return; |
| 55 | +
|
| 56 | + audio.src = streamUrl; |
| 57 | + audio.currentTime = 0; |
34 | 58 |
|
35 | 59 | audio.play(); |
| 60 | + player.update({ playing: true }); |
36 | 61 | } |
37 | 62 |
|
38 | 63 | export function play() { |
39 | 64 | audio.play(); |
40 | | - updateStatus({ playing: true }); |
| 65 | + player.update({ playing: true }); |
41 | 66 | } |
42 | 67 |
|
43 | 68 | export function pause() { |
44 | 69 | audio.pause(); |
45 | | - updateStatus({ playing: false }); |
| 70 | + player.update({ playing: false }); |
46 | 71 | } |
47 | 72 |
|
48 | 73 | export function stop() { |
49 | 74 | audio.src = ''; |
50 | 75 | audio.currentTime = 0; |
51 | | - player.set({ |
52 | | - track: undefined, |
53 | | - playing: false, |
54 | | - currentTime: 0, |
55 | | - duration: 0, |
56 | | - }); |
| 76 | + player.reset(); |
57 | 77 | } |
58 | 78 |
|
59 | 79 | export function skip(seconds: number) { |
60 | 80 | const newTime = clamp(audio.currentTime + seconds, 0, audio.duration); |
61 | 81 | audio.currentTime = audio.currentTime + seconds; |
62 | | - updateStatus({ currentTime: newTime }); |
| 82 | + player.update({ currentTime: newTime }); |
63 | 83 | } |
64 | 84 |
|
65 | 85 | export function skipTo(seconds: number) { |
66 | 86 | const newTime = clamp(seconds, 0, audio.duration); |
67 | 87 | audio.currentTime = seconds; |
68 | | - updateStatus({ currentTime: newTime }); |
69 | | - } |
70 | | -
|
71 | | - function updateStatus(changes: Partial<PlaybackProgress>) { |
72 | | - player.update((a) => ({ |
73 | | - ...a, |
74 | | - ...changes, |
75 | | - })); |
| 88 | + player.update({ currentTime: newTime }); |
76 | 89 | } |
77 | 90 | </script> |
0 commit comments