-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmedia-session.js
68 lines (55 loc) · 2.07 KB
/
media-session.js
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
import { MediaSession } from '@jofr/capacitor-media-session';
const audioElement = document.querySelector('audio');
let playbackStopped = true;
const updatePositionState = () => {
MediaSession.setPositionState({
position: audioElement.currentTime,
duration: audioElement.duration,
playbackRate: audioElement.playbackRate
});
}
audioElement.addEventListener('durationchange', updatePositionState);
audioElement.addEventListener('seeked', updatePositionState);
audioElement.addEventListener('ratechange', updatePositionState);
audioElement.addEventListener('play', updatePositionState);
audioElement.addEventListener('pause', updatePositionState);
const updatePlaybackState = () => {
const playbackState = playbackStopped ? 'none' : (audioElement.paused ? 'paused' : 'playing');
MediaSession.setPlaybackState({
playbackState: playbackState
});
}
audioElement.addEventListener('play', () => {
playbackStopped = false;
updatePlaybackState();
MediaSession.setMetadata({
title: 'Prelude',
artist: 'Jan Morgenstern',
album: 'Big Buck Bunny',
artwork: [
{ src: './assets/imgs/logo.png', type: 'image/png', sizes: '512x512' }
]
});
});
audioElement.addEventListener('pause', updatePlaybackState);
MediaSession.setActionHandler({ action: 'play' }, () => {
audioElement.play();
});
MediaSession.setActionHandler({ action: 'pause' }, () => {
audioElement.pause();
});
MediaSession.setActionHandler({ action: 'seekto' }, (details) => {
audioElement.currentTime = details.seekTime;
});
MediaSession.setActionHandler({ action: 'seekforward' }, (details) => {
const seekOffset = details.seekOffset ?? 30;
audioElement.currentTime = audioElement.currentTime + seekOffset;
});
MediaSession.setActionHandler({ action: 'seekbackward' }, (details) => {
const seekOffset = details.seekOffset ?? 30;
audioElement.currentTime = audioElement.currentTime - 30;
});
MediaSession.setActionHandler({ action: 'stop' }, () => {
playbackStopped = true;
audioElement.pause();
});