-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_player.html
163 lines (150 loc) · 5.89 KB
/
add_player.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
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add and control player</title>
<style>
body
{
font-family: system-ui, sans-serif;
}
hgroup h1,
hgroup h2
{
margin: 0;
}
/* Styling for the container element and iframe that will be added inside. */
div#video
{
position: relative;
/* Keeps the container at a 16:9 aspect ratio no matter its width. */
padding-bottom: 56.25%;
}
div#video > iframe
{
position: absolute;
left: 0;
top: 0;
/* `width: 100%;` and `height: 100%` are set inline by the embed library. */
}
</style>
<!-- Load the Switch Tube embed library asynchronous. -->
<script async src="https://tube.switch.ch/js/embed.js"></script>
</head>
<body>
<hgroup>
<h1>Switch Tube JS embed library</h1>
<h2>Add and control player</h2>
</hgroup>
<p>This example shows how to use the Switch Tube embed library to add a video
player inside an existing container element, and how to control playback. This
is the preferred way to use the embed library from your JavaScript code.</p>
<p>The content of the container element will be replaced with an iframe that
has its width and height set to 100%. Note that the CSS applied in this example
is just one of the many ways in which the size and positioning of the embedded
player can be controlled.</p>
<!-- This element will be used as a container for the player iframe. -->
<div id="video"></div>
<!-- These input elements are used to demonstrate player control. -->
<p>
<input type="button" name="play" value="Play">
<input type="button" name="pause" value="Pause">
<input type="number" name="position" value="0">
</p>
<!-- These buttons demonstrate how to get status details from the player. -->
<p>
<input type="button" name="ready" value="Ready?">
<input type="button" name="playing" value="Playing?">
<input type="button" name="duration" value="Duration?">
<input type="button" name="time" value="Current time?">
</p>
<script>
// Usage of the embed library is wrapped in this function. More on that below.
function addVideo() {
// Find the element to be used as the container for the player iframe.
let container = document.querySelector('div#video')
// Once the player has been loaded inside the iframe, the embed library
// will trigger a `SwitchTubeEmbed:load` event on the container element.
// It is recommended to set up usage of the player control object in
// response to this event.
container.addEventListener('SwitchTubeEmbed:load', (event) => {
// Get the player control object from the event’s `detail` property.
let player = event.detail
// Add event handlers for the input elements that control playback. The
// `play` and `pause` methods start and stop playback and the `seek`
// method changes the current playback time.
document.querySelector('input[name=play]').addEventListener(
'click',
player.play
)
document.querySelector('input[name=pause]').addEventListener(
'click',
player.pause
)
document.querySelector('input[name=position]').addEventListener(
'change',
(event) => {
// Time is in seconds, fractional values are allowed.
player.seek(event.target.value)
}
)
// Add event handlers for the status buttons. The status methods return
// promises that resolves to the value of the requested attribute.
document.querySelector('input[name=ready]').addEventListener(
'click', () => {
// Resolves to `true` when the video is ready for playback, `false`
// otherwise
player.ready().then(alert)
})
document.querySelector('input[name=playing]').addEventListener(
// Resolves to `true` when the video is currently playing, `false`
// when it is paused.
'click', () => {
player.playing().then(alert)
})
document.querySelector('input[name=duration]').addEventListener(
// Resolves to the duration of the video in seconds.
'click', () => {
player.duration().then(alert)
})
document.querySelector('input[name=time]').addEventListener(
// Resolves to the current playback time in seconds.
'click', () => {
player.currentTime().then(alert)
})
})
// With a handler for the `SwitchTubeEmbed:load` added, the player can be
// added and the video loaded. The `player` method requires a reference to
// the container element and a SwitchTube video URL as its arguments.
//
// The `player` method returns the same player control object as is
// available from the `SwitchTubeEmbed:load` event’s `detail` property, but
// this is not used here.
SwitchTubeEmbed.player(
// The reference can be an Element instance, a CSS selector, or an
// element id string. Here we’re using the element assigned above.
container,
// Both the video page as well as the embed URL can be used. The URL can
// include query parameters that control the appearance of the player
// (e.g. `?title=hide` to hide the video title) and a fragment identifier
// can be appended to set the start time (e.g. `#30` to start at 30
// seconds).
'https://tube.switch.ch/videos/d42e0c7f'
)
}
// Because the embed library is loaded asynchronous, it might or might not be
// available when your code runs. This is why its usage is wrapped in the
// `addVideo` function defined above.
if (window.SwitchTubeEmbed) {
// If the embed library is available, call `addVideo` directly.
addVideo()
} else {
// If the embed library is not yet available, add `addVideo` as the event
// handler for the `SwitchTubeEmbed:ScriptLoaded` event. This event will be
// triggered by the embed library when it has finished loading to indicate
// that it is available for usage.
window.addEventListener('SwitchTubeEmbed:ScriptLoaded', addVideo)
}
</script>
</body>
</html>