-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_demo.html
147 lines (128 loc) · 4.07 KB
/
google_demo.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
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<style>
#emotion {
font-size: 32pt;
}
#player {
margin: 0 auto;
}
#timecode {
position:fixed;
top: 10pt;
left: 10pt;
text-align: center;
width: 40pt;
padding: 5pt;
background-color: black;
color: white;
}
</style>
<script src="demo_utils.es6"></script>
<script src="google_demo.js"></script>
<!-- <script src="google_demo_mapping.js"></script> -->
</script>
</head>
<body>
<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="player" class="center-block"></div>
<div id="emotion" class="text-center alert alert-info">*</div>
<div id="timecode">000.0</div>
<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '720',
width: '1280',
videoId: emotions.videoID,
events: {
'onReady': onPlayerReady,
}
});
}
var timeupdater = null;
var clearEmotion = null;
var videotime = 0;
var nextEmotion = 0;
var endEmotion = 0;
// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
event.target.playVideo();
function updateTime() {
var oldTime = videotime;
if(player && player.getCurrentTime) {
videotime = player.getCurrentTime();
}
if(videotime !== oldTime) {
timecode.innerHTML = pad(videotime.toFixed(1),5);
checkEmotion(Math.round(videotime * 1000));
}
}
timeupdater = setInterval(updateTime, 100);
}
// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
var done = false;
function pad(num, size) {
var s = num+"";
while (s.length < size) s = "0" + s;
return s;
}
function checkEmotion(time) {
// reinit index to find next emotion
nextEmotion = 0;
while (time>emotions.triggers[nextEmotion].millis) {
nextEmotion++;
}
// get next emotion from the list
em = emotions.triggers[nextEmotion];
// move to the next emotion
// if missed emotion, skip it
if (em.millis-time>150) {
return;
}
// We hit the next emotion time
triggerEmotion(em);
nextEmotion++;
}
// handle clearing and triggering emotions
function triggerEmotion(em) {
clearTimeout(clearEmotion);
emotion.innerHTML = em.emotion;
handleEmotion(em)
// if no set duration, assign
if (!em.duration) {
em.duration = 2000;
}
// overwrite emotion
clearEmotion = setTimeout(triggerEmotion, em.duration, {"emotion": "*", "duration": 999999999});
}
// get the url and set the port to send messages
var url = server_url;
server_port = "5555";
// get desired emotion and send to robot
function handleEmotion(em) {
// if not null emotion
if(em.emotion!="*") {
// create message with url, server, and gesture
msg = "http://"+url+":"+server_port+"/s/" + em.gesture;
// write to html
emotion.innerHTML = em.emotion + " -> " + msg;
// send
$.get(msg, function(data, status){});
}
}
</script>
</body>
</html>