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

207 lines
5.7 KiB
JavaScript
Raw Normal View History

2011-07-29 18:48:43 +00:00
// vim: et:ts=4:sw=4:sts=4:ft=javascript
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)
2011-04-22 22:03:10 +00:00
.defaults({
2011-08-19 10:45:36 +00:00
autoplay: false,
preload: 'none',
src: []
2011-04-22 22:03:10 +00:00
})
2011-08-19 10:45:36 +00:00
.options(options || {});
self.options.src = Ox.isArray(self.options.src) ? self.options.src : [self.options.src];
Ox.print('VIDEO ELEMENT OPTIONS', self.options)
self.currentPart = 0;
self.duration = 0;
self.durations = self.options.src.map(function() {
return 0;
});
self.offsets = [];
self.parts = self.options.src.length;
self.paused = true;
self.$videos = self.options.src.map(function(src, i) {
return $('<video>')
.css({position: 'absolute'})
2011-04-22 22:03:10 +00:00
.bind({
2011-08-19 10:45:36 +00:00
ended: function() {
if (i < self.parts - 1) {
setCurrentPart(self.currentPart + 1);
self.video.play();
} else {
2011-08-19 14:44:03 +00:00
self.ended = true;
self.paused = true;
2011-08-19 10:45:36 +00:00
that.triggerEvent('ended');
}
},
2011-04-22 22:03:10 +00:00
loadedmetadata: function() {
2011-08-19 10:45:36 +00:00
self.durations[i] = self.videos[i].duration;
Ox.print(i, 'lm', self.durations);
if (Ox.every(self.durations)) {
self.duration = Ox.sum(self.durations);
self.offsets = Ox.range(self.parts).map(function(i) {
return Ox.sum(Ox.sub(self.durations, 0, i));
});
Ox.print('s.o', self.offsets)
that.triggerEvent('loadedmetadata');
}
2011-05-12 10:51:17 +00:00
},
seeked: function() {
that.triggerEvent('seeked');
},
seeking: function() {
that.triggerEvent('seeking');
2011-08-19 10:45:36 +00:00
},
progress: function() {
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
})
.attr(Ox.extend({
preload: 'metadata',
src: src
}, i == 0 && self.options.autoplay ? {
autoplay: 'autoplay'
} : {}))
2011-08-19 10:45:36 +00:00
.hide()
.appendTo(that.$element);
2011-04-22 22:03:10 +00:00
});
2011-08-19 10:45:36 +00:00
self.videos = self.$videos.map(function($video) {
return $video[0];
});
self.$video = self.$videos[0].show();
self.video = self.$video[0];
2011-04-22 22:03:10 +00:00
2011-08-19 10:45:36 +00:00
function getCurrentTime() {
return self.offsets[self.currentPart] + self.video.currentTime;
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]
} 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;
}
function setCurrentPart(part) {
2011-08-19 16:49:00 +00:00
Ox.print('sCP', part);
2011-08-19 10:45:36 +00:00
var css = {};
['left', 'top', 'width', 'height'].forEach(function(key) {
css[key] = self.$video.css(key);
2011-04-22 22:03:10 +00:00
});
2011-08-19 10:45:36 +00:00
self.$video.hide();
self.video.pause();
self.$video = self.$videos[part].css(css).show();
self.video = self.$video[0];
!self.paused && self.video.play();
self.currentPart = part;
Ox.print('sCP', part, self.video.src)
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
function setCurrentTime(time) {
2011-08-19 16:49:00 +00:00
Ox.print('sCT', time);
2011-08-19 10:45:36 +00:00
var currentPart, currentTime;
Ox.loop(self.parts - 1, -1, -1, function(i) {
if (self.offsets[i] <= time) {
currentPart = i;
currentTime = time - self.offsets[i];
return false;
}
});
Ox.print('sCT', time, currentPart, currentTime);
if (currentPart != self.currentPart) {
setCurrentPart(currentPart);
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
self.video.currentTime = currentTime;
}
that.animate = function() {
self.$video.animate.apply(self.$video, arguments);
return that;
}
that.buffered = function() {
return self.video.buffered;
2011-04-22 22:03:10 +00:00
};
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;
};
that.css = function() {
self.$video.css.apply(self.$video, arguments);
2011-04-22 22:03:10 +00:00
return that;
};
2011-08-19 10:45:36 +00:00
that.duration = function() {
return self.duration;
};
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
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;
};
that.play = function() {
2011-08-19 14:44:03 +00:00
if (self.ended) {
that.currentTime(0);
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-08-19 10:45:36 +00:00
that.src = function() {
var ret;
2011-04-22 22:03:10 +00:00
if (arguments.length == 0) {
2011-08-19 10:45:36 +00:00
ret = self.video.src;
2011-04-22 22:03:10 +00:00
} else {
2011-08-19 10:45:36 +00:00
self.options.src = Ox.isArray(arguments[0]) ? arguments[0] : [arguments[0]];
self.videos[currentPart].src = self.options.src[currentPart];
self.videos.forEach(function(video, i) {
if (i != currentPart) {
video.src = self.options.src[i];
}
})
ret = that;
2011-04-22 22:03:10 +00:00
}
2011-08-19 10:45:36 +00:00
return ret;
2011-04-22 22:03:10 +00:00
};
2011-08-19 10:45:36 +00:00
that.videoHeight = function() {
return self.video.videoHeight;
2011-04-22 22:03:10 +00:00
};
2011-08-19 10:45:36 +00:00
that.videoWidth = function() {
return self.video.videoWidth;
};
2011-04-22 22:03:10 +00:00
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;
};