@gumlet/player.js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +370 -0
- package/dist/.keep +0 -0
- package/dist/player.js +1076 -0
- package/dist/player.min.js +1 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Gumlet Pte. Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
Player.js
|
|
2
|
+
=========
|
|
3
|
+
|
|
4
|
+
A JavaScript library for interacting with iframes that support Player.js
|
|
5
|
+
spec.
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
const player = new playerjs.Player('iframe');
|
|
9
|
+
|
|
10
|
+
player.on('ready', () => {
|
|
11
|
+
player.on('play', () => {
|
|
12
|
+
console.log('play');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
player.getDuration(duration => console.log(duration));
|
|
16
|
+
|
|
17
|
+
if (player.supports('method', 'mute')) {
|
|
18
|
+
player.mute();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
player.play();
|
|
22
|
+
});
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Install
|
|
26
|
+
-------
|
|
27
|
+
|
|
28
|
+
Player.js is hosted on JSDelivr's CDN. :
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@gumlet/player.js@1.0/dist/player.min.js"></script>
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
install via npm :
|
|
35
|
+
|
|
36
|
+
```sh
|
|
37
|
+
npm install player.js
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Ready
|
|
41
|
+
-----
|
|
42
|
+
|
|
43
|
+
Because of the dance that we need to do between both iframes, you should
|
|
44
|
+
always wait till the `ready` events to fire before interacting with the
|
|
45
|
+
player object. However, the player will internally queue messages until
|
|
46
|
+
ready is called. :
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
const player = new playerjs.Player('iframe');
|
|
50
|
+
|
|
51
|
+
player.on(playerjs.Events.PLAY, () => console.log('play'));
|
|
52
|
+
|
|
53
|
+
player.on('ready', () => player.setCurrentTime(20));
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Timing
|
|
57
|
+
------
|
|
58
|
+
|
|
59
|
+
The timing between when the iframe is added and when the ready event is
|
|
60
|
+
fired is important. Sadly we cannot fire the ready event till the iframe
|
|
61
|
+
is loaded, but there is no concrete way of telling when postmessage is
|
|
62
|
+
available to us.
|
|
63
|
+
|
|
64
|
+
The best way is to do one of the following.
|
|
65
|
+
|
|
66
|
+
### Create the iframe via JavaScript
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
const iframe = document.createElement('iframe');
|
|
70
|
+
iframe.src = 'https://example.com/iframe';
|
|
71
|
+
document.body.appendChild(iframe);
|
|
72
|
+
|
|
73
|
+
const player = new playerjs.Player(iframe);
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
In this case, Player.js will listen to the onload event of the iframe
|
|
77
|
+
and only try to communicate when ready.
|
|
78
|
+
|
|
79
|
+
### Wait for the document to be ready.
|
|
80
|
+
|
|
81
|
+
```html
|
|
82
|
+
<iframe src="//example.com/iframe"></iframe>
|
|
83
|
+
|
|
84
|
+
<script>
|
|
85
|
+
$(document).on('ready', () => {
|
|
86
|
+
$('iframes').each(() => {
|
|
87
|
+
const player = new playerjs.Player(this);
|
|
88
|
+
player.on('ready', () => player.play());
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
</script>
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
At this point we can reasonably assume that the iframe's been loaded and
|
|
95
|
+
the ready. Player.js will take care of listening for ready events that
|
|
96
|
+
were fired before the player is set up.
|
|
97
|
+
|
|
98
|
+
Methods
|
|
99
|
+
-------
|
|
100
|
+
|
|
101
|
+
`play`: void
|
|
102
|
+
Play the media:
|
|
103
|
+
|
|
104
|
+
```js
|
|
105
|
+
player.play();
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`pause`: void
|
|
109
|
+
Pause the media:
|
|
110
|
+
|
|
111
|
+
```js
|
|
112
|
+
player.pause();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
`getPaused`: boolean
|
|
116
|
+
Determine if the media is paused:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
player.getPaused(function(value){
|
|
120
|
+
console.log('paused:', value);
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`mute`: void
|
|
125
|
+
Mute the media:
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
player.mute();
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
`unmute`: void
|
|
132
|
+
Unmute the media:
|
|
133
|
+
|
|
134
|
+
```js
|
|
135
|
+
player.unmute();
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`getMuted`: boolean
|
|
139
|
+
Determine if the media is muted:
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
player.getMuted(value => console.log('muted:', value));
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
`setVolume`: void
|
|
146
|
+
Set the volume. Value needs to be between 0-100:
|
|
147
|
+
|
|
148
|
+
```
|
|
149
|
+
player.setVolume(50);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`getVolume`: number
|
|
153
|
+
Get the volume. Value will be between 0-100:
|
|
154
|
+
|
|
155
|
+
```js
|
|
156
|
+
player.getVolume(value => console.log('getVolume:', value));
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
`getDuration`: number
|
|
160
|
+
Get the duration of the media is seconds:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
player.getDuration(value => console.log('getDuration:', value));
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
`setCurrentTime`: number
|
|
167
|
+
Perform a seek to a particular time in seconds:
|
|
168
|
+
|
|
169
|
+
```js
|
|
170
|
+
player.setCurrentTime(50);
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`getCurrentTime`: number
|
|
174
|
+
Get the current time in seconds of the video:
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
player.getCurrentTime(value => console.log('getCurrentTime:', value));
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`off`: void
|
|
181
|
+
Remove an event listener. If the listener is specified it should remove
|
|
182
|
+
only that listener, otherwise remove all listeners:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
player.off('play');
|
|
186
|
+
|
|
187
|
+
player.off('play', playCallback);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
`on`: void
|
|
191
|
+
Add an event listener:
|
|
192
|
+
|
|
193
|
+
```js
|
|
194
|
+
player.on('play', () => console.log('play'));
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
`supports`: \['method', 'event'\], methodOrEventName
|
|
198
|
+
Determines if the player supports a given event or method.
|
|
199
|
+
|
|
200
|
+
```js
|
|
201
|
+
player.supports('method', 'getDuration');
|
|
202
|
+
player.supports('event', 'ended');
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
Events
|
|
206
|
+
------
|
|
207
|
+
|
|
208
|
+
Events that can be listened to.
|
|
209
|
+
|
|
210
|
+
`ready`
|
|
211
|
+
fired when the media is ready to receive commands. This is fired
|
|
212
|
+
regardless of listening to the event. Note: As outlined in the PlayerJs
|
|
213
|
+
Spec, you may run into inconsistencies if you have multiple players on
|
|
214
|
+
the page with the same `src`. To get around this, simply append a UUID
|
|
215
|
+
or a timestamp to the iframe's src to guarantee that all players on the
|
|
216
|
+
page have a unique `src`.
|
|
217
|
+
|
|
218
|
+
`progress`
|
|
219
|
+
fires when the media is loading additional media for playback:
|
|
220
|
+
|
|
221
|
+
```js
|
|
222
|
+
{
|
|
223
|
+
percent: 0.8,
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
`timeupdate`
|
|
228
|
+
fires during playback:
|
|
229
|
+
|
|
230
|
+
```js
|
|
231
|
+
data: {
|
|
232
|
+
seconds: 10,
|
|
233
|
+
duration: 40
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
`play`
|
|
238
|
+
fires when the video starts to play.
|
|
239
|
+
|
|
240
|
+
`pause`
|
|
241
|
+
fires when the video is paused.
|
|
242
|
+
|
|
243
|
+
`ended`
|
|
244
|
+
fires when the video is finished.
|
|
245
|
+
|
|
246
|
+
`seeked`
|
|
247
|
+
fires when the video has been seeked by the user.
|
|
248
|
+
|
|
249
|
+
`error`
|
|
250
|
+
fires when an error occurs.
|
|
251
|
+
|
|
252
|
+
Receiver
|
|
253
|
+
--------
|
|
254
|
+
|
|
255
|
+
If you are looking to implement the Player.js spec, we include a
|
|
256
|
+
Receiver that will allow you to easily listen to events and takes care
|
|
257
|
+
of the house keeping.
|
|
258
|
+
|
|
259
|
+
```js
|
|
260
|
+
const receiver = new playerjs.Receiver();
|
|
261
|
+
|
|
262
|
+
receiver.on('play', () => {
|
|
263
|
+
video.play();
|
|
264
|
+
receiver.emit('play');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
receiver.on('pause', () => {
|
|
268
|
+
video.pause();
|
|
269
|
+
receiver.emit('pause');
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
receiver.on('getDuration', callback => callback(video.duration));
|
|
273
|
+
|
|
274
|
+
receiver.on('getVolume', callback => callback(video.volume*100));
|
|
275
|
+
|
|
276
|
+
receiver.on('setVolume', value => video.volume = (value/100));
|
|
277
|
+
|
|
278
|
+
receiver.on('mute', () => video.mute = true)
|
|
279
|
+
|
|
280
|
+
receiver.on('unmute', () => video.mute = false);
|
|
281
|
+
|
|
282
|
+
receiver.on('getMuted', callback => callback(video.mute));
|
|
283
|
+
|
|
284
|
+
receiver.on('getLoop', callback => callback(video.loop));
|
|
285
|
+
|
|
286
|
+
receiver.on('setLoop', value => video.loop = value);
|
|
287
|
+
|
|
288
|
+
video.addEventListener('ended', () => receiver.emit('ended'));
|
|
289
|
+
|
|
290
|
+
video.addEventListener('timeupdate', () => {
|
|
291
|
+
receiver.emit('timeupdate', {
|
|
292
|
+
seconds: video.currentTime,
|
|
293
|
+
duration: video.duration
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
receiver.ready();
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
Methods
|
|
301
|
+
-------
|
|
302
|
+
|
|
303
|
+
`on`
|
|
304
|
+
Requests an event from the video. The above player methods should be
|
|
305
|
+
implemented. If the event expects a return value a callback will be
|
|
306
|
+
passed into the function call:
|
|
307
|
+
|
|
308
|
+
```js
|
|
309
|
+
receiver.on('getDuration', callback => callback(video.duration));
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
Otherwise you can safely ignore any inputs:
|
|
313
|
+
|
|
314
|
+
```js
|
|
315
|
+
receiver.on('play', callback => video.play());
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
`emit`
|
|
319
|
+
Sends events to the parent as long as someone is listing. The above
|
|
320
|
+
player events should be implemented. If a value is expected, it should
|
|
321
|
+
be passed in as the second argument:
|
|
322
|
+
|
|
323
|
+
receiver.emit('timeupdate', { seconds:20, duration: 40 });
|
|
324
|
+
|
|
325
|
+
`ready`
|
|
326
|
+
Once everything is in place and you are ready to start responding to
|
|
327
|
+
events, call this method. It performs some house keeping, along with
|
|
328
|
+
emitting `ready`:
|
|
329
|
+
|
|
330
|
+
```js
|
|
331
|
+
receiver.ready();
|
|
332
|
+
```
|
|
333
|
+
Adapters
|
|
334
|
+
--------
|
|
335
|
+
|
|
336
|
+
In order to make it super easy to add Player.js to any embed, we have
|
|
337
|
+
written adapters for common video libraries. We currently have adapters
|
|
338
|
+
for [Video.js](http://www.videojs.com/),
|
|
339
|
+
[JWPlayer](https://www.jwplayer.com/) and [HTML5
|
|
340
|
+
Video](http://dev.w3.org/html5/spec-author-view/video.html). An Adapter
|
|
341
|
+
wraps the Receiver and wires up all the events so your iframe is
|
|
342
|
+
Player.js compatible.
|
|
343
|
+
|
|
344
|
+
### VideoJSAdapter
|
|
345
|
+
|
|
346
|
+
An adapter for [Video.js](http://www.videojs.com/). :
|
|
347
|
+
|
|
348
|
+
```js
|
|
349
|
+
videojs("video", {}, () => {
|
|
350
|
+
const adapter = new playerjs.VideoJSAdapter(this);
|
|
351
|
+
// ... Do other things to initialize your video.
|
|
352
|
+
|
|
353
|
+
// Start accepting events
|
|
354
|
+
adapter.ready();
|
|
355
|
+
});
|
|
356
|
+
```
|
|
357
|
+
### HTML5Adapter
|
|
358
|
+
|
|
359
|
+
An adapter for [HTML5
|
|
360
|
+
Video](http://dev.w3.org/html5/spec-author-view/video.html). :
|
|
361
|
+
|
|
362
|
+
```js
|
|
363
|
+
const video = document.getElementById('video');
|
|
364
|
+
video.load();
|
|
365
|
+
|
|
366
|
+
const adapter = playerjs.HTML5Adapter(video);
|
|
367
|
+
|
|
368
|
+
// Start accepting events
|
|
369
|
+
adapter.ready();
|
|
370
|
+
```
|
package/dist/.keep
ADDED
|
File without changes
|