oxjs/source/Ox.UI/js/Video/Ox.VideoPlayer.js
2011-05-15 10:35:00 +02:00

1214 lines
No EOL
42 KiB
JavaScript

/*@
Ox.VideoPlayer <f> Generic Video Player
(options, self) -> <o> Video Player
options <o> Options
annotation <[o]> Array of annotation tracks
name <s> Name of the annotation track
data <[o]> Annotation data
in <n> In point (sec)
out <n> Out point (sec)
text <s> Text
controls <[s]> Controls, from left to right
Can be 'play', 'playInToOut', 'mute', 'volume', 'size', 'scale',
'timeline', 'space', 'position', 'settings'. The 'space' control
is just empty space that separates left-aligned from right-aligned
controls
duration <n|-1> Duration (sec)
enableKeyboard <b|false> If true, enable keyboard controls
externalControls <b|false> If true, controls are outside the video
focus <s|'click'> focus on 'click', 'load' or 'mouseover'
fps <n|25> Frames per second
fullscreen <b|false> If true, video is in fullscreen
height <n|144> Height in px (excluding external controls)
in <n> In point (sec)
keepIconVisible <b|false> If true, play icon stays visible after mouseleave
keepLargeTimelineVisible <b|false> If true, large timeline stays visible after mouseleave
keepLogoVisible <b|false> If true, logo stays visible after mouseleave
logo <s|''> Logo image URL
logoLink <s|''> Logo link URL
logoTitle <s|''> Text for tooltip
muted <b|false> If true, video is muted
paused <b|false> If true, video is paused
playInToOut <b|false> If true, video plays only from in to out
position <n|0> Initial position (sec)
poster <s|''> Poster URL
posterFrame <n|-1> Position of poster frame (sec)
preload <s|'auto'> 'auto', 'metadata' or 'none'
out <n|-1> Out point (sec)
scaleToFill <b|false> If true, scale to fill (otherwise, scale to fit)
showFind <b|false> If true, show find input
showHours <b|false> If true, don't show hours for videos shorter than one hour
showIcon <b|false> If true, show play icon
showIconOnLoad <b|false> If true, show icon on load
showInterfaceOnLoad <b|false> If true, show controls and title on load
showLargeTimeline <b|false> If true, show large timeline
showMilliseconds <n|0> Number of decimals to show
showPointMarkers <b|false> If true, show in/out markers
showProgress <|false> If true, show buffering progress
sizeIsLarge <b|false> If true, initial state of the size control is large
subtitles <s|[o]|[]> URL or SRT or array of subtitles
in <n> In point (sec)
out <n> Out point (sec)
text <s> Text
timeline <s> Timeline image URL
title <s|''> Video title
type <s|'play'> 'play', 'in' or 'out'
video <s|''> Video URL
volume <n|1> Volume (0-1)
width <n|256> Width in px
@*/
Ox.VideoPlayer = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
annotations: [],
controls: [],
duration: 86399,
enableKeyboard: false,
externalControls: false,
focus: 'click',
fps: 25,
fullscreen: false,
height: 144,
'in': 0,
keepIconVisible: false,
keepLargeTimelineVisible: false,
keepLogoVisible: false,
logo: '',
logoLink: '',
logoTitle: '',
muted: false,
paused: false,
playInToOut: false,
position: 0,
poster: '',
preload: 'auto',
out: 0,
scaleToFill: false,
showFind: false,
showHours: false,
showIcon: false,
showIconOnLoad: false,
showInterfaceOnLoad: false,
showLargeTimeline: false,
showMilliseconds: 0,
showPointMarkers: false,
showProgress: false,
subtitles: [],
timeline: '',
title: '',
type: 'play',
video: '',
volume: 1,
width: 256
})
.options(options || {})
.addClass('OxVideo')
.css({
position: 'absolute'
});
Ox.UI.$window.bind({
resize: function() {
self.options.fullscreen && setSizes();
}
});
self['in'] = self.options.playInToOut ? self.options['in'] : 0,
self.out = self.options.playInToOut ? self.options.out : self.options.duration;
self.options.duration = self.out - self['in'];
self.options.position = Ox.limit(self.options.position, self['in'], self.out);
Ox.print('p/d', self.options.position, self.options.duration);
// fixme: this is _relative_, resizing can happen
self.millisecondsPerFrame = 1000 / self.options.fps;
self.secondsPerFrame = 1 / self.options.fps;
self.barHeight = 16;
self.width = self.options.fullscreen ? window.innerWidth : self.options.width;
self.height = self.options.fullscreen ? window.innerHeight : self.options.height;
if (self.options.enableKeyboard) {
that.bindEvent({
key_1: function() {
toggleScale();
},
key_left: function() {
setPosition(self.options.position - self.secondsPerFrame, true);
},
key_p: function() {
playInToOut();
},
key_right: function() {
setPosition(self.options.position + self.secondsPerFrame, true);
},
key_shift_f: function() {
toggleFullscreen();
},
key_space: function() {
togglePaused(true);
}
})
}
if (self.options.enableKeyboard) {
if (self.options.focus == 'mouseenter') {
that.bind({
mouseenter: that.gainFocus,
mouseleave: that.loseFocus
});
} else {
that.bind({
click: that.gainFocus
});
}
}
if ((!self.options.externalControls && self.options.controls.length) || self.options.showIcon || self.options.title) {
that.bind({
mouseenter: showInterface,
mouseleave: hideInterface
});
}
if (Ox.isString(self.options.subtitles)) {
if (self.options.subtitles.indexOf('\n') > -1) {
self.options.subtitles = Ox.parseSRT(self.options.subtitles);
} else {
Ox.get(self.options.subtitles, function(data) {
self.options.subtitles = Ox.parseSRT(data);
});
//self.options.subtitles = [];
}
}
self.buffered = [];
self.controlsTimeout;
self.dragTimeout;
self.$videoContainer = $('<div>')
.css({
position: 'absolute',
background: 'rgb(0, 0, 0)',
overflow: 'hidden'
})
.bind({
click: function() {
togglePaused(true);
}
})
.appendTo(that.$element)
self.$video = $('<video>')
.attr(Ox.extend({
preload: self.options.preload,
src: self.options.video
}, !self.options.paused ? {
autoplay: 'autoplay'
} : {}/*, self.options.poster ? {
poster: self.options.poster
} : {}*/))
.css({
position: 'absolute'
})
.bind(Ox.extend({
ended: ended,
loadedmetadata: loadedmetadata,
seeked: seeked,
seeking: seeking
}, self.options.progress ? {
progress: progress
} : {}))
.appendTo(self.$videoContainer);
self.video = self.$video[0];
if (self.options.poster) {
self.$poster = $('<img>')
.attr({
src: self.options.poster
})
.css({
position: 'absolute'
})
.appendTo(self.$videoContainer);
self.posterIsVisible = true;
}
if (self.options.logo) {
self.$logo = $('<img>')
.attr({
src: self.options.logo
})
.css({
position: 'absolute',
cursor: self.options.logoLink ? 'pointer' : 'default',
opacity: 0.25,
})
.appendTo(self.$videoContainer);
if (self.options.logoTitle) {
self.$logoTooltip = Ox.Tooltip({
title: self.options.logoTitle
});
}
}
self.$loadingIcon = $('<img>')
.attr({
src: Ox.UI.getImagePath('symbolLoadingAnimated.svg')
.replace('/classic/', '/modern/')
})
.css({
position: 'absolute'
})
.appendTo(self.$videoContainer);
if (self.options.showIcon || self.options.showIconOnLoad) {
self.$playIcon = $('<img>')
.attr({
src: Ox.UI.getImagePath(
'symbol' + (self.options.paused ? 'Play' : 'Pause') + '.svg'
).replace('/classic/', '/modern/')
})
.css({
position: 'absolute',
border: '2px solid rgb(255, 255, 255)',
background: 'rgba(0, 0, 0, 0.5)',
opacity: 0
})
.appendTo(self.$videoContainer);
if (self.options.showIcon) {
self.$playIcon.addClass('OxInterface');
}
if (self.options.showIconOnLoad) {
self.iconIsVisible = true;
}
}
if (self.options.subtitles.length) {
self.$subtitle = $('<div>')
//.addClass('OxSubtitle')
.css({
position: 'absolute',
left: 0,
right: 0,
textAlign: 'center',
textShadow: 'rgba(0, 0, 0, 1) 0 0 4px',
color: 'rgb(255, 255, 255)'
})
.appendTo(self.$videoContainer);
}
if (self.options.title) {
self.$titlebar = $('<div>')
.addClass('OxInterface')
.css({
position: 'absolute',
height: (self.barHeight - 1) + 'px',
paddingTop: '1px',
textAlign: 'center',
color: 'rgb(255, 255, 255)',
opacity: 0
})
.css({
backgroundImage: '-moz-linear-gradient(top, rgba(64, 64, 64, 0.5), rgba(0, 0, 0, 0.5))'
})
.css({
backgroundImage: '-webkit-linear-gradient(top, rgba(64, 64, 64, 0.5), rgba(0, 0, 0, 0.5))'
})
.html(self.options.title)
.appendTo(that.$element);
}
if (self.options.controls.length) {
self.$controls = Ox.Bar({
size: self.barHeight
})
.addClass('OxInterface')
.css({
position: 'absolute',
bottom: 0,
opacity: self.options.externalControls ? 1 : 0
})
.appendTo(that.$element);
if (!self.options.externalControls) {
self.$controls.css({
backgroundImage: '-moz-linear-gradient(top, rgba(64, 64, 64, 0.5), rgba(0, 0, 0, 0.5))'
})
.css({
backgroundImage: '-webkit-linear-gradient(top, rgba(64, 64, 64, 0.5), rgba(0, 0, 0, 0.5))'
});
}
self.$control = {};
self.options.controls.forEach(function(control) {
if (control == 'play') {
self.$playButton = Ox.Button({
style: 'symbol',
// FIXME: this is retarded, fix Ox.Button
title: [
{id: 'play', title: 'play', selected: self.options.paused},
{id: 'pause', title: 'pause', selected: !self.options.paused}
],
tooltip: ['Play', 'Pause'],
type: 'image'
})
.css({float: 'left'})
.bindEvent('click', function() {
togglePaused();
})
.appendTo(self.$controls);
} else if (control == 'playInToOut') {
self.$playInToOutButton = Ox.Button({
style: 'symbol',
title: 'playInToOut',
tooltip: 'Play In to Out',
type: 'image'
})
.css({float: 'left'})
.bindEvent('click', playInToOut)
.appendTo(self.$controls);
} else if (control == 'mute') {
self.$muteButton = Ox.Button({
style: 'symbol',
title: [
{id: 'mute', title: 'mute', selected: !self.options.muted},
{id: 'unmute', title: 'unmute', selected: self.options.muted}
],
tooltip: ['Mute', 'Unmute'],
type: 'image'
})
.css({float: 'left'})
.bindEvent('click', toggleMuted)
.appendTo(self.$controls);
} else if (control == 'volume') {
} else if (control == 'scale') {
self.$scaleButton = Ox.Button({
style: 'symbol',
title: [
{id: 'fill', title: 'fill', selected: !self.options.scaleToFill},
{id: 'fit', title: 'fit', selected: self.options.scaleToFill}
],
tooltip: ['Scale to Fill', 'Scale to Fit'],
type: 'image'
})
.css({float: 'left'})
.bindEvent('click', toggleScale)
.appendTo(self.$controls);
} else if (control == 'fullscreen') {
self.$fullscreenButton = Ox.Button({
style: 'symbol',
title: [
{id: 'grow', title: 'grow', selected: !self.options.fullscreen},
{id: 'shrink', title: 'shrink', selected: self.options.fullscreen}
],
tooltip: ['Enter Fullscreen', 'Exit Fullscreen'],
type: 'image'
})
.css({float: 'left'})
.bindEvent('click', toggleFullscreen)
.appendTo(self.$controls);
} else if (control == 'size') {
self.$sizeButton = Ox.Button({
style: 'symbol',
title: [
{id: 'grow', title: 'grow', selected: !self.options.sizeIsLarge},
{id: 'shrink', title: 'shrink', selected: self.options.sizeIsLarge}
],
tooltip: ['Larger', 'Smaller'],
type: 'image'
})
.css({float: 'left'})
.bindEvent('click', toggleSize)
.appendTo(self.$controls);
}else if (control == 'timeline') {
self.$timeline = Ox.Element()
.addClass('timeline')
.css({
float: 'left',
height: self.barHeight + 'px',
background: 'rgba(0, 0, 0, 0.75)',
borderRadius: self.barHeight / 2 + 'px'
})
.appendTo(self.$controls);
self.$timelineImages = Ox.Element()
.addClass('timelineimages')
.css({
float: 'left',
height: self.barHeight + 'px',
marginLeft: self.barHeight / 2 + 'px'
})
.appendTo(self.$timeline);
self.$timelineImage = $('<img>')
.addClass('timelineimage')
.attr({
src: self.options.timeline
})
.css({
float: 'left',
height: self.barHeight + 'px'
})
.appendTo(self.$timelineImages.$element);
///*
if (self.options.showProgress) {
self.$progress = $('<img>')
.attr({
src: getProgressImageURL()
})
.css({
float: 'left',
height: self.barHeight + 'px',
})
.appendTo(self.$timelineImages.$element);
}
//*/
self.$positionMarker = $('<div>')
.addClass('positionmarker')
.css({
float: 'left',
width: '14px',
height: '14px',
border: '1px solid rgba(0, 0, 0, 0.5)',
borderRadius: '8px'
})
.append(
self.$positionMarkerRing = $('<div>')
.css({
width: '10px',
height: '10px',
border: '2px solid rgba(255, 255, 255, 0.5)',
borderRadius: '7px',
})
.append(
$('<div>')
.css({
width: '8px',
height: '8px',
border: '1px solid rgba(0, 0, 0, 0.5)',
borderRadius: '5px',
})
)
)
.appendTo(self.$timeline.$element);
self.$timelineInterface = Ox.Element()
.css({
float: 'left',
height: self.barHeight + 'px',
})
.appendTo(self.$controls);
self.$tooltip = Ox.Tooltip({
animate: false
});
} else if (control == 'space') {
self.$space = $('<div>')
.css({float: 'left'})
.html('&nbsp;') // fixme: ??
.appendTo(self.$controls.$element);
} else if (control == 'position') {
self.positionWidth = 48 +
!!self.options.showMilliseconds * 2
+ self.options.showMilliseconds * 6;
self.$position = $('<div>')
.addClass('foo')
.css({
float: 'left',
width: (self.positionWidth - 4) + 'px',
height: '12px',
padding: '2px',
fontSize: '9px',
textAlign: 'center',
color: 'rgb(255, 255, 255)'
})
.html(formatPosition)
.bind({
click: function() {
if (!self.options.paused) {
self.wasPlaying = true;
togglePaused(true);
}
self.$position.hide();
self.$positionInput
.options({
value: formatPosition()
})
.show()
.focusInput(false);
}
})
.appendTo(self.$controls.$element);
self.$positionInput = Ox.Input({
value: formatPosition(),
width: self.positionWidth
})
.css({
float: 'left',
background: 'rgba(0, 0, 0, 0)',
MozBoxShadow: '0 0 0',
WebkitBoxShadow: '0 0 0'
})
.bind({
blur: function() {
},
focus: function() {
}
})
.bindEvent({
focus: function() {
self.inputHasFocus = true;
},
blur: function() {
self.inputHasFocus = false;
submitPositionInput();
}
})
.hide()
.appendTo(self.$controls.$element);
self.$positionInput.children('input').css({
width: (self.positionWidth - 6) + 'px',
height: '16px',
padding: '0 3px 0 3px',
border: '0px',
borderRadius: '8px',
fontSize: '9px',
color: 'rgb(255, 255, 255)'
})
.css({
background: '-moz-linear-gradient(top, rgba(0, 0, 0, 0.5), rgba(64, 64, 64, 0.5))'
})
.css({
background: '-webkit-linear-gradient(top, rgba(0, 0, 0, 0.5), rgba(64, 64, 64, 0.5))'
});
}
});
}
setSizes();
function ended() {
if (!self.options.paused) {
togglePaused(true);
}
if (self.options.poster) {
self.$poster.animate({
opacity: 1
}, 250);
self.posterIsVisible = true;
}
if (self.options.showIconOnLoad) {
self.$playIcon.animate({
opacity: 1
}, 250);
self.iconIsVisible = true;
}
}
function formatPosition(position) {
position = Ox.isUndefined(position) ? self.options.position : position;
return Ox.formatDuration(position, self.options.showMilliseconds);
}
function getCSS(element) {
var css;
if (element == 'controls' || element == 'titlebar') {
css = {
width: self.width + 'px'
};
} else if (element == 'loadingIcon') {
css = {
left: self.iconLeft + 'px',
top: self.iconTop + 'px',
width: self.iconSize + 'px',
height: self.iconSize + 'px'
};
} else if (element == 'logo') {
var logoHeight = Math.round(self.height / 10);
self.logoMargin = Math.round(self.height / 20);
css = {
left: self.logoMargin + 'px',
top: self.logoMargin + 'px',
height: logoHeight + 'px',
};
} else if (element == 'player') {
css = Ox.extend({
width: self.width + 'px',
height: (self.options.fullscreen
? window.innerHeight
: self.height + (
self.options.externalControls
? (!!self.options.controls.length + !!self.options.title) * self.barHeight
: 0)) + 'px'
}, self.options.fullscreen ? {
left: 0,
top: 0
} : {}, self.exitFullscreen ? {
left: self.absoluteOffset.left,
top: self.absoluteOffset.top
} : {});
} else if (element == 'playIcon') {
var playIconPadding = Math.round(self.iconSize * 1/8),
playIconSize = self.iconSize - 2 * playIconPadding - 4;
css = {
left: self.iconLeft + 'px',
top: self.iconTop + 'px',
width: playIconSize + 'px',
height: playIconSize + 'px',
padding: playIconPadding + 'px',
borderRadius: Math.round(self.iconSize / 2) + 'px'
};
} else if (element == 'positionMarker') {
var position = self.options.duration ?
(self.options.position - self['in']) / self.options.duration : 0;
css = {
marginLeft: position * self.timelineImageWidth -
self.timelineImageWidth - 8 + 'px',
};
} else if (element == 'poster' || element == 'video') {
var playerWidth = self.width,
playerHeight = self.height,
playerRatio = playerWidth / playerHeight,
videoWidth = self.video.videoWidth,
videoHeight = self.video.videoHeight,
videoRatio = videoWidth / videoHeight,
videoIsWider = videoRatio > playerRatio,
width, height;
if (self.options.scaleToFill) {
width = videoIsWider ? playerHeight * videoRatio : playerWidth;
height = videoIsWider ? playerHeight : playerWidth / videoRatio;
} else {
width = videoIsWider ? playerWidth : playerHeight * videoRatio;
height = videoIsWider ? playerWidth / videoRatio : playerHeight;
}
width = Math.round(width);
height = Math.round(height);
css = {
width: width + 'px',
height: height + 'px',
marginLeft: parseInt((playerWidth - width) / 2),
marginTop: parseInt((playerHeight - height) / 2)
};
} else if (element == 'progress') {
css = {
width: self.timelineImageWidth + 'px',
marginLeft: -self.timelineImageWidth + 'px'
};
} else if (element == 'subtitle') {
css = {
bottom: parseInt(self.height / 16) + 'px',
width: self.width + 'px',
fontSize: parseInt(self.height / 20) + 'px',
WebkitTextStroke: (self.height / 1000) + 'px rgb(0, 0, 0)'
};
} else if (element == 'space' || element == 'timeline') {
css = {
width: self.timelineWidth + 'px'
};
} else if (element == 'timelineImage' || element == 'timelineImages') {
css = {
width: self.timelineImageWidth + 'px'
};
} else if (element == 'timelineInterface') {
css = {
width: self.timelineWidth + 'px',
marginLeft: -self.timelineWidth + 'px'
};
} else if (element == 'videoContainer') {
css = {
width: self.width + 'px',
height: self.height + 'px'
};
}
return css;
}
function getPosition(e) {
// fixme: no offsetX in firefox???
if ($.browser.mozilla) {
//Ox.print(e, e.layerX - 56)
return Ox.limit(
(e.layerX - 48 - self.barHeight / 2) / self.timelineImageWidth * self.video.duration,
0, self.video.duration
);
} else {
/*Ox.print(e.offsetX, Ox.limit(
(e.offsetX - self.barHeight / 2) / self.timelineImageWidth * self.video.duration,
0, self.video.duration
))*/
return Ox.limit(
(e.offsetX - self.barHeight / 2) / self.timelineImageWidth * self.video.duration,
0, self.video.duration
);
}
}
function getProgressImageURL() {
Ox.print('---', self.timelineImageWidth)
if (!self.timelineImageWidth) return;
var width = self.timelineImageWidth,
height = self.barHeight,
canvas = $('<canvas>')
.attr({
width: width,
height: height
})[0],
context = canvas.getContext('2d'),
imageData, data;
context.fillStyle = 'rgba(255, 0, 0, 0.5)';
context.fillRect(0, 0, width, height);
imageData = context.getImageData(0, 0, width, height),
data = imageData.data;
self.buffered.forEach(function(range) {
var left = Math.round(range[0] * width / self.video.duration),
right = Math.round(range[1] * width / self.video.duration);
Ox.loop(left, right, function(x) {
Ox.loop(height, function(y) {
index = x * 4 + y * 4 * width;
data[index + 3] = 0;
});
});
});
context.putImageData(imageData, 0, 0);
return canvas.toDataURL();
}
function getSubtitle() {
var subtitle = '';
Ox.forEach(self.options.subtitles, function(v) {
if (
v['in'] <= self.options.position &&
v.out > self.options.position
) {
subtitle = v.text;
return false;
}
});
return subtitle;
}
function hideInterface() {
if (!self.inputHasFocus) {
self.controlsTimeout = setTimeout(function() {
that.find('.OxInterface').animate({
opacity: 0
}, 250);
self.$logo && self.$logo.animate({
top: self.logoMargin + 'px',
opacity: 0.25
}, 250, function() {
self.options.logoLink && self.$logo.unbind('click');
self.options.logoTitle &&
self.$logo.unbind('mouseenter').unbind('mouseleave');
});
self.$subtitle && self.$subtitle.animate({
bottom: parseInt(self.options.height / 16) + 'px',
});
}, 1000);
}
}
function hideLoadingIcon() {
self.$loadingIcon.hide().attr({
src: Ox.UI.getImagePath('symbolLoading.svg')
.replace('/classic/', '/modern/')
});
}
function loadedmetadata() {
self.loaded = true;
self.out = self.options.playInToOut &&
self.options.out < self.video.duration ?
self.options.out : self.video.duration;
self.options.duration = self.out - self['in'];
self.video.currentTime = self.options.position;
self.$video.css(getCSS('video'));
self.$poster && self.$poster.css(getCSS('poster'));
hideLoadingIcon();
if (self.options.showIcon || self.options.showIconOnLoad) {
//!self.options.keepIconVisible && self.$playIcon.addClass('OxInterface');
if (self.options.showIconOnLoad) {
self.$playIcon.animate({
opacity: 1
}, 250);
}
}
if (self.options.enableKeyboard && self.options.focus == 'load') {
that.gainFocus();
}
self.$timeline && self.$timelineInterface
.bind({
mousedown: mousedownTrack,
mouseleave: mouseleaveTrack,
mousemove: mousemoveTrack,
})
.bindEvent({
drag: dragTrack,
dragpause: dragpauseTrack,
dragend: dragpauseTrack
});
}
function dragTrack(e) {
setPosition(getPosition(e), true);
if (self.dragTimeout) {
clearTimeout(self.dragTimeout);
self.dragTimeout = 0;
}
}
function dragpauseTrack(e) {
self.video.currentTime = self.options.position;
}
function mousedownTrack(e) {
setPosition(getPosition(e), true);
}
function mouseleaveTrack(e) {
self.$tooltip.hide();
}
function mousemoveTrack(e) {
self.$tooltip.options({
title: formatPosition(getPosition(e))
}).show(e);
}
function parsePositionInput(str) {
var split = str.split(':').reverse();
while (split.length > 3) {
split.pop();
}
return split.reduce(function(prev, curr, i) {
return prev + (parseFloat(curr) || 0) * Math.pow(60, i);
}, 0);
}
function playing() {
self.options.position = self.video.currentTime;
if (
(self.options.playInToOut || self.playInToOut) &&
self.options.position >= self.options.out
) {
togglePaused(true);
setPosition(self.options.out/*, true*/);
//ended();
self.playInToOut = false;
} else {
setPosition(self.options.position);
}
}
function playInToOut() {
self.playInToOut = true;
setPosition(self.options['in'], true);
if (self.options.paused) {
togglePaused(true);
}
}
function progress() {
var buffered = self.video.buffered;
for (var i = 0; i < buffered.length; i++) {
self.buffered[i] = [buffered.start(i), buffered.end(i)];
// fixme: firefox weirdness
if (self.buffered[i][0] > self.buffered[i][1]) {
self.buffered[i][0] = 0;
}
}
self.$progress.attr({
src: getProgressImageURL()
});
}
function seeked() {
clearTimeout(self.seekTimeout);
hideLoadingIcon();
self.$playIcon && self.$playIcon.show();
}
function seeking() {
self.seekTimeout = setTimeout(function() {
self.$playIcon && self.$playIcon.hide();
showLoadingIcon();
}, 250);
}
function setPosition(position, setVideo) {
position = Ox.limit(position, self['in'], self['out']);
self.options.position = Math.round(
position * self.options.fps
) / self.options.fps;
if (setVideo && self.loaded) {
self.video.currentTime = self.options.position;
}
if (self.iconIsVisible) {
self.$playIcon.animate({
opacity: 0
}, 250);
self.iconIsVisible = false;
}
if (self.posterIsVisible) {
self.$poster.animate({
opacity: 0
}, 250);
self.posterIsVisible = false;
}
self.$subtitle && setSubtitle();
self.$timeline && self.$positionMarker.css(getCSS('positionMarker'));
self.$position && self.$position.html(formatPosition());
}
function setSizes(callback) {
var ms = callback ? 250 : 0;
self.width = self.options.fullscreen ? window.innerWidth : self.options.width;
self.height = self.options.fullscreen ? window.innerHeight : self.options.height;
self.iconSize = Math.max(Math.round(self.height / 10), 16),
self.iconLeft = parseInt((self.width - self.iconSize) / 2),
self.iconTop = parseInt((self.height - self.iconSize) / 2);
if (self.$timeline || self.$space) {
self.timelineWidth = self.width - self.options.controls.reduce(function(prev, curr) {
return prev + (
curr == 'timeline' || curr == 'space' ? 0 :
curr == 'position' ? self.positionWidth : 16
);
}, 0);
if (self.$timeline) {
self.timelineImageWidth = self.timelineWidth - self.barHeight;
}
}
that.animate(getCSS('player'), ms, callback);
self.$videoContainer.animate(getCSS('videoContainer'), ms);
self.loaded && self.$video.animate(getCSS('video'), ms);
self.$poster && self.$poster.animate(getCSS('poster'), ms);
self.$logo && self.$logo.animate(getCSS('logo'), ms);
self.$loadingIcon.animate(getCSS('loadingIcon'), ms);
self.$playIcon && self.$playIcon.animate(getCSS('playIcon'), ms);
self.$subtitle && self.$subtitle.animate(getCSS('subtitle'), ms);
self.$titlebar && self.$titlebar.animate(getCSS('titlebar'), ms);
self.$controls && self.$controls.animate(getCSS('controls'), ms);
if (self.$timeline) {
self.$timeline.animate(getCSS('timeline'), ms);
self.$timelineImages.animate(getCSS('timelineImages'), ms);
self.$timelineImage.animate(getCSS('timelineImage'), ms);
self.$progress && self.$progress.animate(getCSS('progress'), ms);
self.$positionMarker.animate(getCSS('positionMarker'), ms);
self.$timelineInterface.animate(getCSS('timelineInterface'), ms);
}
self.$space && self.$space.animate(getCSS('space'), ms);
}
function setSubtitle() {
var subtitle = getSubtitle();
if (subtitle != self.subtitle) {
self.subtitle = subtitle;
self.$subtitle.html(
Ox.highlight(self.subtitle, self.options.find, 'Ox.Highlight')
.replace(/\n/g, '<br/>')
);
}
}
function showInterface() {
clearTimeout(self.controlsTimeout);
that.find('.OxInterface').animate({
opacity: 1
}, 250);
self.$logo && self.$logo.animate({
top: self.logoMargin + 16 + 'px',
opacity: 0.5
}, 250, function() {
self.options.logoLink && self.$logo
.bind({
click: function() {
document.location.href = self.options.logoLink;
}
});
self.options.logoTitle && self.$logo
.bind({
mouseenter: function(e) {
self.$logoTooltip.show(e);
},
mouseleave: self.$logoTooltip.hide
});
});
self.$subtitle.animate({
bottom: self.barHeight + parseInt(self.options.height / 16) + 'px',
});
}
function showLoadingIcon() {
self.$loadingIcon.attr({
src: Ox.UI.getImagePath('symbolLoadingAnimated.svg')
.replace('/classic/', '/modern/')
}).show();
}
function submitPositionInput() {
self.$positionInput.hide();
self.$position.html('').show();
setPosition(parsePositionInput(self.$positionInput.options('value')), true);
if (self.wasPlaying) {
togglePaused(true);
self.video.play();
self.wasPlaying = false;
}
}
function toggleFullscreen() {
var parentOffset, wasPlaying;
self.options.fullscreen = !self.options.fullscreen;
if (!self.options.paused) {
self.video.pause();
wasPlaying = true;
}
if (self.options.fullscreen) {
self.$parent = that.parent();
parentOffset = self.$parent.offset();
self.absoluteOffset = that.offset();
self.relativeOffset = {
left: self.absoluteOffset.left - parentOffset.left,
top: self.absoluteOffset.top - parentOffset.top
};
that.detach()
.css({
left: self.absoluteOffset.left + 'px',
top: self.absoluteOffset.top + 'px',
zIndex: 1000
})
.appendTo(Ox.UI.$body);
setSizes(function() {
wasPlaying && self.video.play();
});
} else {
self.exitFullscreen = true;
setSizes(function() {
that.detach()
.css({
left: self.relativeOffset.left + 'px',
top: self.relativeOffset.top + 'px'
})
.appendTo(self.$parent);
wasPlaying && self.video.play();
self.exitFullscreen = false;
});
}
}
function toggleMuted() {
self.options.muted = !self.options.muted;
self.video.muted = self.options.muted;
}
function togglePaused(togglePlayButton) {
self.options.paused = !self.options.paused;
self.$timeline && self.$positionMarkerRing.css({
borderColor: 'rgba(255, 255, 255, ' + (self.options.paused ? 0.5 : 1) + ')'
});
if (self.options.paused) {
self.video.pause();
clearInterval(self.playInterval);
if (self.options.showIcon) {
togglePlayIcon();
self.options.showIcon && self.$playIcon.animate({
opacity: 1
}, 250);
}
} else {
if (self.options.playInToOut && self.options.position > self.options.out - self.secondsPerFrame) {
setPosition(self.options['in'], true);
}
self.video.play();
self.playInterval = setInterval(playing, self.millisecondsPerFrame);
if (self.options.showIcon) {
self.options.showIcon && self.$playIcon.animate({
opacity: 0
}, 250, togglePlayIcon);
}
}
if (togglePlayButton && self.$playButton) {
self.$playButton.toggleTitle();
}
}
function togglePlayIcon() {
self.$playIcon.attr({
src: Ox.UI.getImagePath(
'symbol' + (self.options.paused ? 'Play' : 'Pause'
)+ '.svg').replace('/classic/', '/modern/')
});
}
function toggleScale() {
self.options.scaleToFill = !self.options.scaleToFill;
self.$video.animate(getCSS('video'), 250);
self.$poster && self.$poster.animate(getCSS('poster'), 250);
}
function toggleSize() {
self.options.sizeIsLarge = !self.options.sizeIsLarge;
that.triggerEvent('size', {
size: self.options.sizeIsLarge ? 'large' : 'small'
});
}
self.setOption = function(key, value) {
if (key == 'fullscreen') {
toggleFullscreen();
} else if (key == 'height' || key == 'width') {
setSizes();
} else if (key == 'muted') {
toggleMuted();
self.$muteButton && self.$muteButton.toggleTitle();
} else if (key == 'paused') {
togglePaused(true);
} else if (key == 'position') {
setPosition(value);
} else if (key == 'scaleToFill') {
self.$video.css(getVideoCSS());
}
};
that.playInToOut = function() {
playInToOut();
return that;
};
return that;
};