Commit dc267e89 by Valera Rozuvan

Enabled play/pause buttons for HTML5 video player.

parent 4163bb7b
...@@ -19,7 +19,7 @@ this.HTML5Video = (function () { ...@@ -19,7 +19,7 @@ this.HTML5Video = (function () {
* *
* 'height': 390, * 'height': 390,
* *
* 'videoSources': null, // An object of with properties being video sources. The property name is the * 'videoSources': {}, // An object of with properties being video sources. The property name is the
* // video format of the source. Supported video formats are: 'mp4', 'webm', and * // video format of the source. Supported video formats are: 'mp4', 'webm', and
* // 'ogg'. By default videoSources property is null. This means that the * // 'ogg'. By default videoSources property is null. This means that the
* // player will initialize, and not play anything. If you do not provide a * // player will initialize, and not play anything. If you do not provide a
...@@ -53,12 +53,14 @@ this.HTML5Video = (function () { ...@@ -53,12 +53,14 @@ this.HTML5Video = (function () {
* } * }
*/ */
function Player(el, config) { function Player(el, config) {
var sourceStr, _this;
if (typeof el === 'string') { if (typeof el === 'string') {
this.el = $(el); this.el = $(el);
} else if ($.isPlainObject(el) === true) { } else if (el instanceof jQuery) {
this.el = el; this.el = el;
} else { } else {
// Error. el parameter is required. // Error. Parameter el does not have a recognized type.
// TODO: Make sure that nothing breaks if one of the methods available via this object's prototype // TODO: Make sure that nothing breaks if one of the methods available via this object's prototype
// is called after we return. // is called after we return.
...@@ -69,22 +71,99 @@ this.HTML5Video = (function () { ...@@ -69,22 +71,99 @@ this.HTML5Video = (function () {
if ($.isPlainObject(config) === true) { if ($.isPlainObject(config) === true) {
this.config = config; this.config = config;
} else { } else {
this.config = { // Error. Parameter config does not have a recognized type.
'width': 640,
'height': 390, // TODO: Make sure that nothing breaks if one of the methods available via this object's prototype
'videoSource': '', // is called after we return.
'playerVars': {
'controls': 1, return;
'start': null,
'end': null
},
'events': {
'onReady': null,
'onStateChange': null,
'onPlaybackQualityChange': null
}
};
} }
sourceStr = {
'mp4': ' ',
'webm': ' ',
'ogg': ' '
};
_this = this;
$.each(sourceStr, function (videoType, videoSource) {
if (
(_this.config.videoSources.hasOwnProperty(videoType) === true) &&
(typeof _this.config.videoSources[videoType] === 'string') &&
(_this.config.videoSources[videoType].length > 0)
) {
sourceStr[videoType] =
'<source ' +
'src="' + _this.config.videoSources[videoType] + '" ' +
'type="video/' + videoType + '" ' +
'/> ';
}
});
this.playerState = HTML5Video.PlayerState.UNSTARTED;
this.videoEl = $(
'<video style="width: 100%;">' +
sourceStr.mp4 +
sourceStr.webm +
sourceStr.ogg +
'</video>'
);
this.video = this.videoEl[0];
this.video.addEventListener('canplay', function () {
console.log('We got a "canplay" event.');
_this.playerState = HTML5Video.PlayerState.PAUSED;
if ($.isFunction(_this.config.events.onReady) === true) {
console.log('Callback function "onReady" is defined.');
_this.config.events.onReady({});
}
}, false);
this.video.addEventListener('play', function () {
console.log('We got a "play" event.');
_this.playerState = HTML5Video.PlayerState.PLAYING;
if ($.isFunction(_this.config.events.onStateChange) === true) {
console.log('Callback function "onStateChange" is defined.');
_this.config.events.onStateChange({
'data': _this.playerState
});
}
}, false);
this.video.addEventListener('pause', function () {
console.log('We got a "pause" event.');
_this.playerState = HTML5Video.PlayerState.PAUSED;
if ($.isFunction(_this.config.events.onStateChange) === true) {
console.log('Callback function "onStateChange" is defined.');
_this.config.events.onStateChange({
'data': _this.playerState
});
}
}, false);
this.video.addEventListener('ended', function () {
console.log('We got a "ended" event.');
_this.playerState = HTML5Video.PlayerState.ENDED;
if ($.isFunction(_this.config.events.onStateChange) === true) {
console.log('Callback function "onStateChange" is defined.');
_this.config.events.onStateChange({
'data': _this.playerState
});
}
}, false);
this.videoEl.appendTo(this.el.find('.video-player div'));
} }
/* /*
...@@ -122,7 +201,9 @@ this.HTML5Video = (function () { ...@@ -122,7 +201,9 @@ this.HTML5Video = (function () {
}; };
Player.prototype.pauseVideo = function () { Player.prototype.pauseVideo = function () {
console.log('Player.prototype.pauseVideo');
this.video.pause();
}; };
Player.prototype.seekTo = function () { Player.prototype.seekTo = function () {
...@@ -160,21 +241,15 @@ this.HTML5Video = (function () { ...@@ -160,21 +241,15 @@ this.HTML5Video = (function () {
}; };
Player.prototype.playVideo = function () { Player.prototype.playVideo = function () {
console.log('Player.prototype.playVideo');
this.video.play();
}; };
Player.prototype.getPlayerState = function () { Player.prototype.getPlayerState = function () {
}; };
Player.prototype.pauseVideo = function () {
};
Player.prototype.setVolume = function () {
};
Player.prototype.getVolume = function () { Player.prototype.getVolume = function () {
}; };
......
class @VideoPlayerAlpha extends SubviewAlpha class @VideoPlayerAlpha extends SubviewAlpha
initialize: -> initialize: ->
if @video.videoType is 'youtube' if @video.videoType is 'youtube'
@PlayerState = YT.PlayerState
# Define a missing constant of Youtube API # Define a missing constant of Youtube API
YT.PlayerState.UNSTARTED = -1 @PlayerState.UNSTARTED = -1
else if @video.videoType is 'html5'
@PlayerState = HTML5Video.PlayerState
@currentTime = 0 @currentTime = 0
@el = $("#video_#{@video.id}") @el = $("#video_#{@video.id}")
...@@ -51,7 +54,7 @@ class @VideoPlayerAlpha extends SubviewAlpha ...@@ -51,7 +54,7 @@ class @VideoPlayerAlpha extends SubviewAlpha
# work in AS3, not HMLT5. but iframe use AS3 # work in AS3, not HMLT5. but iframe use AS3
@playerVars.end = @video.end @playerVars.end = @video.end
if @video.videoType is 'html5' if @video.videoType is 'html5'
@player = new HTML5Video.Player @video.id, @player = new HTML5Video.Player @video.el,
playerVars: @playerVars, playerVars: @playerVars,
videoSources: @video.html5Sources, videoSources: @video.html5Sources,
events: events:
...@@ -80,13 +83,13 @@ class @VideoPlayerAlpha extends SubviewAlpha ...@@ -80,13 +83,13 @@ class @VideoPlayerAlpha extends SubviewAlpha
onStateChange: (event) => onStateChange: (event) =>
switch event.data switch event.data
when YT.PlayerState.UNSTARTED when @PlayerState.UNSTARTED
@onUnstarted() @onUnstarted()
when YT.PlayerState.PLAYING when @PlayerState.PLAYING
@onPlay() @onPlay()
when YT.PlayerState.PAUSED when @PlayerState.PAUSED
@onPause() @onPause()
when YT.PlayerState.ENDED when @PlayerState.ENDED
@onEnded() @onEnded()
onPlaybackQualityChange: (event, value) => onPlaybackQualityChange: (event, value) =>
...@@ -168,12 +171,16 @@ class @VideoPlayerAlpha extends SubviewAlpha ...@@ -168,12 +171,16 @@ class @VideoPlayerAlpha extends SubviewAlpha
# Delegates # Delegates
play: => play: =>
console.log 'Play clicked'
console.log @player.playVideo
@player.playVideo() if @player.playVideo @player.playVideo() if @player.playVideo
isPlaying: -> isPlaying: ->
@player.getPlayerState() == YT.PlayerState.PLAYING @player.getPlayerState() == @PlayerState.PLAYING
pause: => pause: =>
console.log 'Pause clicked'
console.log @player.pauseVideo
@player.pauseVideo() if @player.pauseVideo @player.pauseVideo() if @player.pauseVideo
duration: -> duration: ->
......
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
<h2> ${display_name} </h2> <h2> ${display_name} </h2>
% endif % endif
<!-- data-streams="0.75:aHUgdwyxTF0,1.0:yJzQiemCIuY,1.25:ELCdMiV1tCQ,1.5:-7gIpfrQdAI"-->
%if settings.MITX_FEATURES['STUB_VIDEO_FOR_TESTING']: %if settings.MITX_FEATURES['STUB_VIDEO_FOR_TESTING']:
<div id="stub_out_video_for_testing"></div> <div id="stub_out_video_for_testing"></div>
%else: %else:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment