oxjs/source/Ox.UI/js/Video/VideoElement.js

453 lines
13 KiB
JavaScript
Raw Normal View History

2011-11-05 16:46:53 +00:00
'use strict';
2011-09-14 14:34:33 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.VideoElement <f> VideoElement Object
options <o> Options object
2013-07-09 22:48:22 +00:00
autoplay <b|false> autoplay
items <a|[]> array of objects with src,in,out,duration,offset
look <b|false> loop playback
self <o> Shared private variable
2012-05-31 10:32:54 +00:00
([options[, self]]) -> <o:Ox.Element> VideoElement Object
2012-06-17 22:38:26 +00:00
loadedmetadata <!> loadedmetadata
2013-07-09 22:48:22 +00:00
itemchange <!> itemchange
2012-06-17 22:38:26 +00:00
seeked <!> seeked
seeking <!> seeking
sizechange <!> sizechange
ended <!> ended
2011-09-14 14:34:33 +00:00
@*/
2011-04-22 22:03:10 +00:00
Ox.VideoElement = function(options, self) {
self = self || {};
2011-08-19 10:45:36 +00:00
var that = Ox.Element({}, self)
2012-12-09 00:42:57 +00:00
.defaults({
autoplay: false,
2013-07-09 22:48:22 +00:00
loop: false,
items: []
2012-12-09 00:42:57 +00:00
})
.options(options || {})
2013-07-09 22:48:22 +00:00
.update({
items: function() {
self.loadedMetadata = false;
loadItems(function() {
var update = true;
self.numberOfItems = self.items.lenth;
if (self.currentItem > self.numberOfItems) {
self.currentItem = 0;
}
if (self.currentItemId != self.items[self.currentItem].id) {
// check if current item is in new items
self.items.some(function(item, i) {
if (item.id == self.currentItemId) {
self.currentItem = i;
loadNextVideo();
update = false;
return true;
}
});
if (update) {
2013-07-09 22:48:22 +00:00
self.currentItem = 0;
self.currentItemId = self.items[self.currentItem].id;
setCurrentVideo();
2013-07-09 22:48:22 +00:00
}
}
that.triggerEvent('durationchanged');
2013-07-09 22:48:22 +00:00
});
}
})
2012-12-09 00:42:57 +00:00
.css({width: '100%', height: '100%'});
2011-08-19 10:45:36 +00:00
Ox.Log('Video', 'VIDEO ELEMENT OPTIONS', self.options);
2011-08-19 10:45:36 +00:00
2013-07-09 22:48:22 +00:00
self.currentItem = 0;
self.currentTime = 0;
self.currentVideo = 0;
self.loadedMetadata = false;
2011-08-19 10:45:36 +00:00
self.paused = true;
2013-07-09 22:48:22 +00:00
self.seeking = false;
self.$videos = [getVideo(), getVideo()];
self.$video = self.$videos[self.currentVideo];
self.video = self.$video[0];
self.$brightness = $('<div>').css({
width: '100%',
height: '100%',
background: 'rgb(0, 0, 0)',
opacity: 0
})
.appendTo(that);
2011-08-20 09:48:28 +00:00
2013-07-09 22:48:22 +00:00
loadItems(function() {
setCurrentItem(0);
self.options.autoplay && play();
});
2011-04-22 22:03:10 +00:00
2011-08-19 10:45:36 +00:00
function getCurrentTime() {
2013-07-09 22:48:22 +00:00
var item = self.items[self.currentItem];
return self.seeking
? self.currentTime
: item.offset + self.video.currentTime - item['in'];
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
function getset(key, value) {
var ret;
if (Ox.isUndefined(value)) {
ret = self.video[key];
2011-08-19 10:45:36 +00:00
} else {
self.video[key] = value;
ret = that;
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
return ret;
}
2013-07-09 22:48:22 +00:00
function getVideo() {
return $('<video>')
.css({position: 'absolute'})
.on({
ended: function() {
if (self.video == this) {
setCurrentItem(self.currentItem + 1);
}
},
loadedmetadata: function() {
// metadata loaded in loadItems
},
progress: function() {
// not implemented
},
seeked: function() {
if (self.video == this) {
2011-08-20 09:48:28 +00:00
that.triggerEvent('seeked');
2013-07-09 22:48:22 +00:00
self.seeking = false;
}
},
seeking: function() {
//seeking event triggered in setCurrentTime
},
stop: function() {
if (self.video == this) {
self.video.pause();
2011-08-20 09:48:28 +00:00
that.triggerEvent('ended');
}
2013-07-09 22:48:22 +00:00
},
timeupdate: function() {
if (self.video == this) {
if (self.items[self.currentItem].out && this.currentTime >= self.items[self.currentItem].out) {
setCurrentItem(self.currentItem + 1);
}
}
}
2011-10-22 21:03:42 +00:00
})
2013-07-09 22:48:22 +00:00
.attr({
preload: 'auto'
})
.hide()
.appendTo(that);
2011-08-20 09:48:28 +00:00
}
2013-07-09 22:48:22 +00:00
function loadItems(callback) {
var currentTime = 0, i =0,
items = self.options.items.map(function(item) {
return Ox.isObject(item) ? Ox.clone(item, true) : {src: item};
});
next();
function getId(item) {
return item.src + '/' + item['in'] + '-' + item.out;
}
2013-07-09 22:48:22 +00:00
function next() {
var item;
if (i < items.length) {
item = items[i];
item['in'] = item['in'] || 0;
item.offset = currentTime;
if (item.out) {
item.duration = item.out - item['in'];
}
if (item.duration) {
if(!item.out) {
item.out = item.duration - item['in'];
}
console.log('add item', item);
currentTime += item.duration;
item.id = getId(item);
2013-07-09 22:48:22 +00:00
i++;
next()
} else {
Ox.Log('VIDEO', 'getVideoInfo', item.src);
Ox.getVideoInfo(item.src, function(info) {
item.duration = info.duration;
if(!item.out) {
item.out = item['in'] + item.duration;
}
2013-07-09 22:48:22 +00:00
currentTime += item.duration;
item.id = getId(item);
2013-07-09 22:48:22 +00:00
i++;
next();
});
2013-07-09 22:48:22 +00:00
}
} else {
self.loadedMetadata = true;
self.items = items;
self.numberOfItems = self.items.length;
callback && callback();
onLoadedMetadata(self.$video, function() {
that.triggerEvent('loadedmetadata');
});
2013-07-09 22:48:22 +00:00
}
}
}
2013-07-09 22:48:22 +00:00
function loadNextVideo() {
var item = self.items[self.currentItem],
nextItem = Ox.mod(self.currentItem + 1, self.numberOfItems),
next = self.items[nextItem],
$nextVideo = self.$videos[Ox.mod(self.currentVideo + 1, self.$videos.length)],
nextVideo = $nextVideo[0];
nextVideo.src = next.src;
onLoadedMetadata($nextVideo, function() {
nextVideo.currentTime = next['in'] || 0;
});
}
2013-07-09 22:48:22 +00:00
function onLoadedMetadata($video, callback) {
if ($video[0].readyState) {
callback();
} else {
$video.one('loadedmetadata', function(event) {
callback();
});
}
}
2011-08-20 09:48:28 +00:00
function setCurrentItem(item) {
2013-07-09 22:48:22 +00:00
Ox.Log('Video', 'sCI', item, self.numberOfItems);
2011-08-20 09:48:28 +00:00
var interval;
2013-07-09 22:48:22 +00:00
if(item >= self.numberOfItems) {
if (self.options.loop) {
item = Ox.mod(item, self.numberOfItems);
} else {
self.ended = true;
self.paused = true;
self.video && self.video.pause();
that.triggerEvent('ended');
return;
}
2011-08-20 09:48:28 +00:00
}
2013-07-09 22:48:22 +00:00
self.video && self.video.pause();
//fixme always sync now?
set();
2011-08-20 09:48:28 +00:00
function set() {
self.currentItem = item;
self.currentItemId = self.items[self.currentItem].id;
2013-07-09 22:48:22 +00:00
setCurrentVideo();
onLoadedMetadata(self.$video, function() {
2011-08-20 09:48:28 +00:00
that.triggerEvent('sizechange');
2013-07-09 22:48:22 +00:00
that.triggerEvent('itemchange', {
item: self.currentItem
});
});
2011-08-20 09:48:28 +00:00
}
}
2013-07-09 22:48:22 +00:00
function setCurrentVideo() {
var css = {},
muted = false,
2013-07-09 22:48:22 +00:00
volume = 1,
item = self.items[self.currentItem],
next;
Ox.Log('Video', 'sCV', item);
2011-08-19 10:45:36 +00:00
['left', 'top', 'width', 'height'].forEach(function(key) {
2013-07-09 22:48:22 +00:00
css[key] = self.$videos[self.currentVideo].css(key);
2011-04-22 22:03:10 +00:00
});
2011-08-20 09:48:28 +00:00
if (self.video) {
2013-07-09 22:48:22 +00:00
self.$videos[self.currentVideo].hide();
self.video.pause();
volume = self.video.volume;
muted = self.video.muted;
2011-08-20 09:48:28 +00:00
}
2013-07-09 22:48:22 +00:00
self.currentVideo = Ox.mod(self.currentVideo + 1, self.$videos.length);
self.$video = self.$videos[self.currentVideo];
2011-08-19 10:45:36 +00:00
self.video = self.$video[0];
2013-07-09 22:48:22 +00:00
if (self.$video.attr('src') != item.src) {
self.video.src = item.src;
}
self.video.volume = volume;
self.video.muted = muted;
2013-07-09 22:48:22 +00:00
self.$video.css(css).show();
2011-08-19 10:45:36 +00:00
!self.paused && self.video.play();
2013-07-09 22:48:22 +00:00
Ox.Log('Video', 'sCV', self.video.src);
if (item['in']) {
setCurrentItemTime(item['in']);
}
loadNextVideo();
}
function setCurrentItemTime(currentTime) {
Ox.Log('Video', 'sCIT', currentTime);
onLoadedMetadata(self.$video, function() {
self.video.currentTime = currentTime;
});
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
function setCurrentTime(time) {
2011-11-04 15:54:28 +00:00
Ox.Log('Video', 'sCT', time);
2013-07-09 22:48:22 +00:00
var currentTime, currentItem;
self.items.forEach(function(item, i) {
if (time >= item.offset
&& time < item.offset + item.duration) {
currentItem = i;
currentTime = time - item.offset + item['in'];
return false;
2011-08-19 10:45:36 +00:00
}
});
2013-07-09 22:48:22 +00:00
// Set to end of items if time > duration
if(Ox.isUndefined(currentItem) && Ox.isUndefined(currentTime)) {
currentItem = self.items.length -1;
currentTime = self.items[currentItem].duration + self.items[currentItem]['in'];
2011-04-22 22:03:10 +00:00
}
2013-07-09 22:48:22 +00:00
Ox.Log('Video', 'sCT', time, '=>', currentItem, currentTime);
if (currentItem != self.currentItem) {
setCurrentItem(currentItem);
}
2013-07-09 22:48:22 +00:00
self.seeking = true;
self.currentTime = time;
that.triggerEvent('seeking');
setCurrentItemTime(currentTime);
2011-08-20 09:48:28 +00:00
}
2011-09-14 14:34:33 +00:00
/*@
animate <f> animate
@*/
2011-08-19 10:45:36 +00:00
that.animate = function() {
self.$video.animate.apply(self.$video, arguments);
return that;
2011-09-14 14:34:33 +00:00
};
2011-08-19 10:45:36 +00:00
2011-10-22 21:03:42 +00:00
/*@
brightness <f> get/set brightness
@*/
that.brightness = function() {
var ret;
if (arguments.length == 0) {
ret = 1 - parseFloat(self.$brightness.css('opacity'));
} else {
self.$brightness.css({opacity: 1 - arguments[0]});
ret = that;
}
return ret;
};
2011-09-14 14:34:33 +00:00
/*@
buffered <f> buffered
@*/
2011-08-19 10:45:36 +00:00
that.buffered = function() {
return self.video.buffered;
2011-04-22 22:03:10 +00:00
};
2011-09-14 14:34:33 +00:00
/*@
currentTime <f> get/set currentTime
@*/
2011-08-19 10:45:36 +00:00
that.currentTime = function() {
var ret;
2011-08-19 14:44:03 +00:00
self.ended = false;
2011-08-19 10:45:36 +00:00
if (arguments.length == 0) {
ret = getCurrentTime();
} else {
setCurrentTime(arguments[0]);
ret = that;
}
return ret;
};
2011-09-14 14:34:33 +00:00
/*@
css <f> css
@*/
2011-08-19 10:45:36 +00:00
that.css = function() {
2013-07-09 22:48:22 +00:00
self.$video.css.apply(self.$video, arguments);
2011-04-22 22:03:10 +00:00
return that;
};
2011-09-14 14:34:33 +00:00
/*@
duration <f> duration
@*/
2011-08-19 10:45:36 +00:00
that.duration = function() {
2013-07-09 22:48:22 +00:00
return self.items ? Ox.sum(self.items.map(function(item) {
return item.duration;
})) : NaN;
2011-08-19 10:45:36 +00:00
};
2011-09-14 14:34:33 +00:00
/*@
muted <f> get/set muted
@*/
2011-04-22 22:03:10 +00:00
that.muted = function() {
2011-08-19 10:45:36 +00:00
return getset('muted', arguments[0]);
};
2011-04-22 22:03:10 +00:00
2011-09-14 14:34:33 +00:00
/*@
pause <f> pause
@*/
2011-04-22 22:03:10 +00:00
that.pause = function() {
2011-08-19 10:45:36 +00:00
self.paused = true;
2011-04-22 22:03:10 +00:00
self.video.pause();
return that;
};
2011-09-14 14:34:33 +00:00
/*@
play <f> play
@*/
2011-04-22 22:03:10 +00:00
that.play = function() {
2011-08-19 14:44:03 +00:00
if (self.ended) {
that.currentTime(0);
}
2013-07-09 22:48:22 +00:00
self.ended = false;
2011-08-19 10:45:36 +00:00
self.paused = false;
2011-04-22 22:03:10 +00:00
self.video.play();
return that;
};
2011-09-14 14:34:33 +00:00
/*@
playNext <f> play next
@*/
2011-08-20 09:48:28 +00:00
that.playNext = function() {
Ox.Log('Video', 'PLAY NEXT');
2011-08-20 09:48:28 +00:00
setCurrentItem(self.currentItem + 1);
self.video.play();
};
2011-09-14 14:34:33 +00:00
/*@
playPrevious <f> play previous
@*/
2011-08-20 09:48:28 +00:00
that.playPrevious = function() {
2013-07-09 22:48:22 +00:00
Ox.Log('Video', 'PLAY PREVIOUS');
2011-08-20 09:48:28 +00:00
setCurrentItem(self.currentItem - 1);
2013-07-09 22:48:22 +00:00
self.video.play();
2011-04-22 22:03:10 +00:00
};
2011-09-14 14:34:33 +00:00
/*@
videoHeight <f> get videoHeight
@*/
2011-08-19 10:45:36 +00:00
that.videoHeight = function() {
return self.video.videoHeight;
2011-04-22 22:03:10 +00:00
};
2011-09-14 14:34:33 +00:00
/*@
videoWidth <f> get videoWidth
@*/
2011-08-19 10:45:36 +00:00
that.videoWidth = function() {
return self.video.videoWidth;
};
2011-04-22 22:03:10 +00:00
2011-09-14 14:34:33 +00:00
/*@
volume <f> get/set volume
@*/
2011-08-19 10:45:36 +00:00
that.volume = function(value) {
return getset('volume', arguments[0]);
};
2011-04-22 22:03:10 +00:00
return that;
};