/**************************************************************************** Copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ /** * @class * @extends ccui.Widget * @brief Displays a video file. * * @note VideoPlayer displays a video file based on DOM element * VideoPlayer will be rendered above all other graphical elements. * * @property {String} path - The video path */ ccui.VideoPlayer = ccui.Widget.extend(/** @lends ccui.VideoPlayer# */{ _played: false, _playing: false, _stopped: true, ctor: function(path){ ccui.Widget.prototype.ctor.call(this); this._EventList = {}; if(path) this.setURL(path); }, _createRenderCmd: function(){ return new ccui.VideoPlayer.RenderCmd(this); }, /** * Set the video address * Automatically replace extname * All supported video formats will be added to the video * @param {String} address */ setURL: function(address){ this._renderCmd.updateURL(address); }, /** * Get the video path * @returns {String} */ getURL: function() { return this._renderCmd._url; }, /** * Play the video */ play: function(){ var self = this, video = this._renderCmd._video; if(video){ this._played = true; video.pause(); if(this._stopped !== false || this._playing !== false || this._played !== true) video.currentTime = 0; if(ccui.VideoPlayer._polyfill.autoplayAfterOperation){ setTimeout(function(){ video.play(); self._playing = true; self._stopped = false; }, 20); }else{ video.play(); this._playing = true; this._stopped = false; } } }, /** * Pause the video */ pause: function(){ var video = this._renderCmd._video; if(video && this._playing === true && this._stopped === false){ video.pause(); this._playing = false; } }, /** * Resume the video */ resume: function(){ if(this._stopped === false && this._playing === false && this._played === true){ this.play(); } }, /** * Stop the video */ stop: function(){ var self = this, video = this._renderCmd._video; if(video){ video.pause(); video.currentTime = 0; this._playing = false; this._stopped = true; } setTimeout(function(){ self._dispatchEvent(ccui.VideoPlayer.EventType.STOPPED); }, 0); }, /** * Jump to the specified point in time * @param {Number} sec */ seekTo: function(sec){ var video = this._renderCmd._video; if(video){ video.currentTime = sec; if(ccui.VideoPlayer._polyfill.autoplayAfterOperation && this.isPlaying()){ setTimeout(function(){ video.play(); }, 20); } } }, /** * Whether the video is playing * @returns {boolean} */ isPlaying: function(){ if(ccui.VideoPlayer._polyfill.autoplayAfterOperation && this._playing){ setTimeout(function(){ video.play(); }, 20); } return this._playing; }, /** * Whether to keep the aspect ratio */ setKeepAspectRatioEnabled: function(enable){ cc.log("On the web is always keep the aspect ratio"); }, isKeepAspectRatioEnabled: function(){ return false; }, /** * Set whether the full screen * May appear inconsistent in different browsers * @param {boolean} enable */ setFullScreenEnabled: function(enable){ var video = this._renderCmd._video; if(video){ if(enable) cc.screen.requestFullScreen(video); else cc.screen.exitFullScreen(video); } }, /** * Determine whether already full screen */ isFullScreenEnabled: function(){ cc.log("Can't know status"); }, /** * The binding event * @param {ccui.VideoPlayer.EventType} event * @param {Function} callback */ setEventListener: function(event, callback){ this._EventList[event] = callback; }, /** * Delete events * @param {ccui.VideoPlayer.EventType} event */ removeEventListener: function(event){ this._EventList[event] = null; }, _dispatchEvent: function(event) { var callback = this._EventList[event]; if (callback) callback.call(this, this, this._renderCmd._video.src); }, /** * Trigger playing events */ onPlayEvent: function(){ var list = this._EventList[ccui.VideoPlayer.EventType.PLAYING]; if(list) for(var i=0; i