-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-session.html
157 lines (131 loc) · 4.49 KB
/
media-session.html
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-selector/iron-selector.html">
<!--
`media-session`
The MediaSession interface of Media Session API implemented in Polymer.
@demo demo/index.html
-->
<dom-module id="media-session">
<template>
<style>
:host {
display: block;
}
</style>
<content id="media" select="#media"></content>
<iron-selector id="selector" items="{{items}}" selected="[[selectedMetadata]]" selected-attribute="active">
<content id="metadata" select="media-metadata"></content>
</iron-selector>
</template>
<script>
(function() {
Polymer({
is: 'media-session',
/**
* The action intent is to resume the playback.
*
* @event play
*/
/**
* The action intent is to pause a currently active playback.
*
* @event pause
*/
/**
* The action intent is to move the playback time backward by a short period (eg. few seconds).
*
* @event seekbackward
*/
/**
* The action intent is to move the playback time forward by a short period (eg. few seconds).
*
* @event seekforward
*/
/**
* The action intent is to either start the current playback from the beginning if the playback has a notion of beginning or move to the previous item in the playlist if the playback has a notion of playlist.
*
* @event previoustrack
*/
/**
* The action is to move to the playback to the next item in the playlist if the playback has a notion of playlist.
*
* @event nexttrack
*/
/**
* Media Session API is not supported.
*
* @event unsupported
*/
properties: {
/**
* The playbackState attribute represents the declared playback state of the media session,
* by which the session declares whether its browsing context is playing media or not.
*/
playbackState: String,
skipTime: Number,
selected: {
type: Number,
notify: true
}
},
attached: function() {
this.media = this.media || Polymer.dom(this.$.media).getDistributedNodes()[0];
if (this.media) {
this.listen(this.media, 'play', 'onPlay');
}
if ('mediaSession' in navigator) {
this._setActionHandler('play');
this._setActionHandler('pause');
if (this.skipTime) {
this._setActionHandler('seekbackward');
this._setActionHandler('seekforward');
}
this.listen(this.$.selector, 'items-changed', 'registerPlaylistActions');
} else {
this.dispatchEvent(new CustomEvent('unsupported'));
}
},
registerPlaylistActions: function(event) {
if (event.detail.value.length) {
this._setActionHandler('previoustrack');
this._setActionHandler('nexttrack');
this.unlisten(this.$.selector, 'items-changed', 'registerPlaylistActions');
}
},
/**
* Convert an action
*/
_setActionHandler: function(action) {
navigator.mediaSession.setActionHandler(action, function() {
this.dispatchEvent(new CustomEvent(action));
this[action]();
}.bind(this));
this.dispatchEvent(new CustomEvent('action-registered', {detail: action}));
},
onPlay: function() {
this.set('selectedMetadata', this.selected);
},
play: function() {
return this.media.play();
},
pause: function() {
this.media.pause();
},
seekbackward: function() {
this.media.currentTime = Math.max(this.media.currentTime - this.skipTime, 0);
},
seekforward: function() {
this.media.currentTime = Math.min(this.media.currentTime + this.skipTime, this.media.duration);
},
previoustrack: function() {
this.selected = (this.selected - 1 + this.$.selector.items.length) % this.$.selector.items.length;
return this.play();
},
nexttrack: function() {
this.selected = (this.selected + 1) % this.$.selector.items.length;
return this.play();
}
});
})(Polymer);
</script>
</dom-module>