-
Notifications
You must be signed in to change notification settings - Fork 1
/
YouTubeInsideWeb.html
155 lines (149 loc) · 5.06 KB
/
YouTubeInsideWeb.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
<!DOCTYPE html>
<html>
<head>
<style>
.video-container {
position: relative;
padding-bottom: 56.25%;
padding-top: 0px;
height: 0;
overflow: hidden;
}
.video-container iframe,
.video-container object,
.video-container embed {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
::-webkit-scrollbar {
display: none;
}
::-webkit-scrollbar-button {
display: none;
}
body {
-ms-overflow-style: none;
}
</style>
</head>
<body bgcolor="#000000">
<div id="playerContainer" class="video-container">
<div id="player"></div>
</div>
<script>
// 文件: https://developers.google.com/youtube/iframe_api_reference
// 滑鼠滑過這個網站後,會 call window.external.notify("mousemove");
document.addEventListener("mousemove", function (e) { window.external.notify("mousemove"); });
// 手勢點擊
document.addEventListener("touchstart", function (e) { window.external.notify("touchstart"); });
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
// 要把 videoId 帶給 query parameter `video`
var videoId = parseQueryParameter('video');
player = new YT.Player('player', {
videoId: videoId,
playerVars: { 'autoplay': 1, 'controls': 0, 'rel': 0, 'showinfo': 0, 'fs': 0, 'disablekb': 1, 'iv_load_policy': 3 },
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
function parseQueryParameter(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
// PlayerReady callback
function onPlayerReady(event) {
player.playVideo();
updateVideoVolume();
}
// 狀態改變的 callback
function onPlayerStateChange(event) {
switch (getPlatform()) {
case "Android":
window.KKBOXYoutube.onPlayerStateChange(event.data);
break;
case "Windows":
switch (event.data) {
case YT.PlayerState.PLAYING:
window.external.notify("play");
break;
case YT.PlayerState.ENDED:
window.external.notify("ended");
break;
default:
window.external.notify("pause");
break;
}
break;
}
}
// 調整尺寸
function setSize(width, height) {
player.setSize(width, height);
}
// 請外部 app 更新音量
function updateVideoVolume() {
window.external.notify("updateVideoVolume");
}
// 靜音
function setVideoMute() {
player.mute();
}
// 解除靜音
function setVideoUnmute() {
player.unMute();
}
// 設定音量
function setVideoVolume(volume) {
player.setVolume(volume * 100);
}
// 暫停
function pauseVideo() {
player.pauseVideo();
}
// 開始播放
function playVideo() {
player.playVideo();
}
// 取得 duration
function getVideoDuration() {
switch (getPlatform()) {
case "Android":
window.KKBOXYoutube.getVideoDuration(player.getDuration());
break;
case "Windows":
return player.getDuration().toString();
}
}
// 取得目前時間
function getVideoCurrentTime() {
switch (getPlatform()) {
case "Android":
window.KKBOXYoutube.getVideoCurrentTime(player.getCurrentTime());
break;
case "Windows":
return player.getCurrentTime().toString();
}
}
// Seek
function setVideoCurrentTime(time) {
player.seekTo(time);
}
function getPlatform() {
var platform = navigator.userAgent.match(/android|windows|mac\ os/i) || [];
return platform[0];
}
</script>
</body>
</html>