-
Notifications
You must be signed in to change notification settings - Fork 0
/
resound.js
122 lines (103 loc) · 3.27 KB
/
resound.js
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
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var video, audio;
var videoURL = '9tG-xwv0kw0';
var musicURL = 'feA64wXhbjo';
var audioStartTime = 0;
var videoStartTime = 0;
var audioEndTime = 0;
var videoEndTime = 0;
function onYouTubePlayerAPIReady() {
var parts = location.search.substring(1).split('&');
var params = {};
// extract possible "params" from the URL. It looks like a get request and walks like a
// get request but I'm really just reading strings....
for (var i = 0; i < parts.length; i++) {
var nv = parts[i].split('=');
if (!nv[0]) continue;
params[nv[0]] = nv[1] || true;
}
videoURL = params.video || videoURL;
musicURL = params.audio || musicURL;
audioStartTime = params.audiostart || audioStartTime;
audioEndTime = params.audioend || audioEndTime;
videoStartTime = params.videostart || videoStartTime;
videoEndTime = params.videoend || videoEndTime;
video = new YT.Player('ytplayer-video', {
videoId: videoURL,
height: "100%",
width: "100%",
playerVars: {
enablejsapi: 1,
iv_load_policy: 3,
modestbranding: 1,
controls: 0,
start:videoStartTime,
end: videoEndTime,
rel: 0,
autoplay: 1,
showinfo: 0,
disablekb: 0,
mute: 1
},
events: {
'onReady': onVideoPlayerReady,
'onStateChange': onVideoStateChange
}
});
audio = new YT.Player('ytplayer-audio', {
videoId: musicURL,
playerVars: {
enablejsapi: 1,
iv_load_policy: 3,
modestbranding: 1,
controls: 0,
start:audioStartTime,
end:audioEndTime,
rel: 0,
autoplay: 1,
showinfo: 0,
disablekb: 1
},
events: {
'onReady': onAudioPlayerReady,
'onStateChange': onAudioStateChange
}
});
}
function onVideoPlayerReady(event) {
event.target.playVideo();
}
function onAudioPlayerReady(event) {
event.target.playVideo();
}
function onAudioStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
audio.seekTo(audioStartTime);
event.target.playVideo();
}
}
// make the audio player act like the video player
function onVideoStateChange(event) {
if (event.data == YT.PlayerState.PAUSED) {
// this could also be mute. idk
audio.pauseVideo();
}
if (event.data == YT.PlayerState.PLAYING) {
// or unmute....
audio.playVideo();
}
if (event.data == YT.PlayerState.ENDED) {
audio.seekTo(audioStartTime);
event.target.seekTo(videoStartTime);
video.playVideo();
}
}
// prevents the user from seeing anything below the main video player.
// this is really hackish but the youtube iframe won't play audio
// if the video isn't visible on the page
window.onscroll = function () { window.scrollTo(0, 0); };
// favorites:
// ?audiostart=20&video=cIwTYL1fwJk&videostart=20&videoend=40