-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch2_cache.html
More file actions
331 lines (293 loc) · 13.2 KB
/
switch2_cache.html
File metadata and controls
331 lines (293 loc) · 13.2 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html>
<html lang="en">
<head>
<title>switch</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #000000;
margin: 0px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
color: #ffffff;
padding: 5px;
font-family:Monospace;
font-size:13px;
font-weight: bold;
text-align:center;
}
a {
color: #ffffff;
}
</style>
</head>
<body>
<div id="ctrl" style="position: fixed;width: 70px;height: 20px;right: 0px; bottom:10px; border: 10px solid #888;text-align: center;padding: 8px; z-index: 9; cursor: pointer; color: white; font-size: 20px;">
PLAY
</div>
<div id="top" style="position: fixed;top:0px; height: 80; overflow: hidden;"></div>
<div id="bot" style="position: fixed;height: 200px; bottom: 0px; color:red;pointer-events: none;"></div>
<div id="container" style="z-index: 999"></div>
<script src="/javascripts/three/three.js"></script>
<script src="/javascripts/three/DeviceOrientationControls.js"></script>
<script src="/javascripts/three/stats.min.js"></script>
<script>
var $e = function (a){
return "string"==typeof a ? document.getElementById(a) : a;
}
var camera, scene, renderer, material, clock, stats, time, mesh, controlDevice, controlTap;
var bot = $e("bot");
var container = $e( 'container' );
var ctrl = $e("ctrl");
var play1 = true;
var currentTime = 0;
var switchDelay = 0.5;
var isSwitching = false;
var video, video2, ttCover;
var curTT;
var isPC = (function(){
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
})();
function log(txt){
bot.innerHTML = txt + "<br/>" + bot.innerHTML;
}
function createVideo(src){
var v = document.createElement( 'video' );
v.height = 80;
v.loop = true;
v.autoplay = true;
//v.controls = "controls";
v.src = src;
v.setAttribute( 'webkit-playsinline', '' );
v.setAttribute( 'playsinline', '' );
$e("top").appendChild(v);
v.addEventListener("timeupdate", function(){
if (v.currentTime > currentTime){
//it never go back
currentTime = v.currentTime;
}
else{
//v.currentTime = currentTime + switchDelay;
}
});
v.addEventListener("loadedmetadata", function(){
log(this.getAttribute("bitrate") + " loadedmetadata");
if (isSwitching)
v.currentTime = currentTime + switchDelay;
});
//var lastSeek = 0;
v.addEventListener("seeked", function(){
//可以通过循环seek达到模拟播放的目的,但是延迟太大达不到流畅播放的效果
// var gap = (Date.now() - lastSeek)/1000.0;
// if (gap > 1)
// gap = 0.1;
// log(this.getAttribute("bitrate") + " seeked:" + v.currentTime.toFixed(2) + " " + gap);
// lastSeek = Date.now();
// v.currentTime = v.currentTime + gap;
if (isSwitching){
log(this.getAttribute("bitrate") + " seeked, start play");
v.play();
}
});
v.addEventListener("playing", function(){
if (isSwitching){
isSwitching = false;
switchDelay = 0.1 + (Date.now() - timer)/1000.0;
log("Finished switching to " + this.getAttribute("bitrate") + ", cost " + (Date.now() - timer));
if(v == video){
video2.pause();
}
else{
video.pause();
}
}
material.map = curTT;
});
return v;
}
var timer;
function switchPlaying(){
isSwitching = true;
play1 = !play1;
timer = Date.now();
if(play1){
log("Start to switch to " + video.getAttribute("bitrate"));
//video.pause();
video.currentTime = currentTime + switchDelay;
video2.play();
curTT = tt;
}
else{
log("Start to switch to " + video2.getAttribute("bitrate"));
//video2.pause();
video2.currentTime = currentTime + switchDelay;
video.play();
curTT = tt2;
//clearInterval(time);
}
}
//init video
var src1, src2;
if (window.location.href.indexOf("1080") > 0){
src1 = "images/af720.mp4";
src2 = "images/af1080.mp4";
ttCover = new THREE.TextureLoader().load( 'images/waterfall.png' );
switchDelay = 0.8;
}
else{
src1 = "images/360.mp4";
src2 = "images/720.mp4";
ttCover = new THREE.TextureLoader().load( 'images/benz.png' );
}
video = createVideo(src1);
video2 = createVideo(src2);
video.setAttribute("bitrate", "v1");
video2.setAttribute("bitrate", "v2");
var tt = new THREE.VideoTexture( video );
tt.minFilter = THREE.LinearFilter;
tt.format = THREE.RGBFormat;
var tt2 = new THREE.VideoTexture( video2 );
tt2.minFilter = THREE.LinearFilter;
tt2.format = THREE.RGBFormat;
curTT = tt;
//these are the pc web controls
var isUserInteracting = false,
onMouseDownMouseX = 0, onMouseDownMouseY = 0,
lon = 0, onMouseDownLon = 0,
lat = 0, onMouseDownLat = 0,
phi = 0, theta = 0,
distance = 500,
onPointerDownPointerX = 0,
onPointerDownPointerY = 0,
onPointerDownLon = 0,
onPointerDownLat = 0;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1100 );//摄像机角度
camera.target = new THREE.Vector3( 0, 0, 0 );
scene = new THREE.Scene();
var geometry = new THREE.SphereBufferGeometry( 500, 60, 60 );//半径,水平分片,垂直分片
geometry.scale( - 1, 1, 1 );
material = new THREE.MeshBasicMaterial( { map : ttCover } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
if(!isPC)
controlDevice = new THREE.DeviceOrientationControls(camera);
//帧率统计
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.bottom = '0px';
stats.domElement.style.zIndex = 10;
document.body.appendChild( stats.domElement );
var isFirstPlaying = true;
ctrl.addEventListener("click", function(){
if (time){
ctrl.innerHTML = "PLAY";
clearInterval(time);
time = null;
video.pause();
video2.pause();
isFirstPlaying = true;
}
else{
ctrl.innerHTML = "STOP";
//trigger both videos to play
video.play();
video2.play();
var playingHandler2 = function(e){
log("video2 cold started");
video2.pause();
video.src = src1;
video.play();
//material.map = tt;
time = setInterval(function(){
log("loop " + play1);
switchPlaying();
}, 5000);
video2.removeEventListener("playing", playingHandler2);
};
if (isFirstPlaying){
video2.addEventListener("playing", playingHandler2);
isFirstPlaying = false;
}
}
});
}
function initPC(){
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'wheel', onDocumentMouseWheel, false );
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function onDocumentMouseDown( event ) {
event.preventDefault();
isUserInteracting = true;
onPointerDownPointerX = event.clientX;
onPointerDownPointerY = event.clientY;
onPointerDownLon = lon;
onPointerDownLat = lat;
}
function onDocumentMouseMove( event ) {
if ( isUserInteracting === true ) {
lon = ( onPointerDownPointerX - event.clientX ) * 0.1 + onPointerDownLon;
lat = ( event.clientY - onPointerDownPointerY ) * 0.1 + onPointerDownLat;
}
}
function onDocumentMouseUp( event ) {
isUserInteracting = false;
}
function onDocumentMouseWheel( event ) {
distance += event.deltaY * 0.05;
}
}
if (isPC){
initPC();
}
function animate() {
requestAnimationFrame( animate );
update();
}
function update() {
if (isPC){
lat = Math.max( - 85, Math.min( 85, lat ) );
phi = THREE.Math.degToRad( 90 - lat );
theta = THREE.Math.degToRad( lon );
camera.position.x = distance * Math.sin( phi ) * Math.cos( theta );
camera.position.y = distance * Math.cos( phi );
camera.position.z = distance * Math.sin( phi ) * Math.sin( theta );
camera.lookAt( camera.target );
}
else{
controlDevice.update();
}
stats.update();
renderer.render( scene, camera );
}
</script>
</body>
</html>