mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
@@ -1,5 +1,3 @@
|
||||
/*global _ccsg */
|
||||
|
||||
/****************************************************************************
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
http://www.cocos.com
|
||||
@@ -20,7 +18,9 @@
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
require('../videoplayer/CCSGVideoPlayer');
|
||||
|
||||
let VideoPlayerImpl = require('../videoplayer/video-player-impl');
|
||||
|
||||
/**
|
||||
* !#en Video event type
|
||||
* !#zh 视频事件类型
|
||||
@@ -61,7 +61,7 @@ require('../videoplayer/CCSGVideoPlayer');
|
||||
* !#zh 视频准备好了,可以开始播放了
|
||||
* @property {Number} READY_TO_PLAY
|
||||
*/
|
||||
var EventType = _ccsg.VideoPlayer.EventType;
|
||||
var EventType = VideoPlayerImpl.EventType;
|
||||
|
||||
|
||||
/**
|
||||
@@ -171,13 +171,13 @@ var VideoPlayer = cc.Class({
|
||||
tooltip: CC_DEV && 'i18n:COMPONENT.videoplayer.currentTime',
|
||||
type: cc.Float,
|
||||
set: function (time) {
|
||||
if (this._sgNode) {
|
||||
this._sgNode.seekTo(time);
|
||||
if (this._impl) {
|
||||
this._impl.seekTo(time);
|
||||
}
|
||||
},
|
||||
get: function () {
|
||||
if (this._sgNode) {
|
||||
return this._sgNode.currentTime();
|
||||
if (this._impl) {
|
||||
return this._impl.currentTime();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
@@ -235,7 +235,7 @@ var VideoPlayer = cc.Class({
|
||||
default: true,
|
||||
type: cc.Boolean,
|
||||
notify: function () {
|
||||
this._sgNode.setKeepAspectRatioEnabled(this.keepAspectRatio);
|
||||
this._impl.setKeepAspectRatioEnabled(this.keepAspectRatio);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -249,7 +249,7 @@ var VideoPlayer = cc.Class({
|
||||
default: false,
|
||||
type: cc.Boolean,
|
||||
notify: function () {
|
||||
this._sgNode.setFullScreenEnabled(this.isFullscreen);
|
||||
this._impl.setFullScreenEnabled(this.isFullscreen);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -269,102 +269,110 @@ var VideoPlayer = cc.Class({
|
||||
ResourceType: ResourceType
|
||||
},
|
||||
|
||||
onLoad: function () {
|
||||
if (CC_JSB) {
|
||||
if (cc.sys.os === cc.sys.OS_OSX || cc.sys.os === cc.sys.OS_WINDOWS) {
|
||||
this.enabled = false;
|
||||
}
|
||||
}
|
||||
ctor () {
|
||||
this._impl = new VideoPlayerImpl();
|
||||
},
|
||||
|
||||
_syncVolume: function () {
|
||||
var sgNode = this._sgNode;
|
||||
if (sgNode) {
|
||||
_syncVolume () {
|
||||
var impl = this._impl;
|
||||
if (impl) {
|
||||
var volume = this._mute ? 0 : this._volume;
|
||||
sgNode.setVolume(volume);
|
||||
impl.setVolume(volume);
|
||||
}
|
||||
},
|
||||
|
||||
_createSgNode: function () {
|
||||
if (CC_JSB) {
|
||||
if (cc.sys.os === cc.sys.OS_OSX || cc.sys.os === cc.sys.OS_WINDOWS) {
|
||||
console.log('VideoPlayer is not supported on Mac and Windows!');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return new _ccsg.VideoPlayer();
|
||||
},
|
||||
|
||||
_updateVideoSource: function () {
|
||||
var sgNode = this._sgNode;
|
||||
_updateVideoSource () {
|
||||
var impl = this._impl;
|
||||
if (this.resourceType === ResourceType.REMOTE) {
|
||||
if (cc.loader.md5Pipe) {
|
||||
this.remoteURL = cc.loader.md5Pipe.transformURL(this.remoteURL);
|
||||
}
|
||||
sgNode.setURL(this.remoteURL);
|
||||
impl.setURL(this.remoteURL);
|
||||
}
|
||||
else {
|
||||
sgNode.setURL(this._clip || '');
|
||||
impl.setURL(this._clip || '');
|
||||
}
|
||||
},
|
||||
|
||||
_initSgNode: function () {
|
||||
var sgNode = this._sgNode;
|
||||
if (sgNode) {
|
||||
if (!CC_JSB) {
|
||||
sgNode.createDomElementIfNeeded();
|
||||
}
|
||||
onLoad () {
|
||||
var impl = this._impl;
|
||||
if (impl) {
|
||||
impl.createDomElementIfNeeded();
|
||||
this._updateVideoSource();
|
||||
|
||||
sgNode.seekTo(this.currentTime);
|
||||
sgNode.setKeepAspectRatioEnabled(this.keepAspectRatio);
|
||||
sgNode.setFullScreenEnabled(this.isFullscreen);
|
||||
sgNode.setContentSize(this.node.getContentSize());
|
||||
impl.seekTo(this.currentTime);
|
||||
impl.setKeepAspectRatioEnabled(this.keepAspectRatio);
|
||||
impl.setFullScreenEnabled(this.isFullscreen);
|
||||
this.pause();
|
||||
|
||||
if (!CC_EDITOR) {
|
||||
sgNode.setEventListener(EventType.PLAYING, this.onPlaying.bind(this));
|
||||
sgNode.setEventListener(EventType.PAUSED, this.onPasued.bind(this));
|
||||
sgNode.setEventListener(EventType.STOPPED, this.onStopped.bind(this));
|
||||
sgNode.setEventListener(EventType.COMPLETED, this.onCompleted.bind(this));
|
||||
sgNode.setEventListener(EventType.META_LOADED, this.onMetaLoaded.bind(this));
|
||||
sgNode.setEventListener(EventType.CLICKED, this.onClicked.bind(this));
|
||||
sgNode.setEventListener(EventType.READY_TO_PLAY, this.onReadyToPlay.bind(this));
|
||||
impl.setEventListener(EventType.PLAYING, this.onPlaying.bind(this));
|
||||
impl.setEventListener(EventType.PAUSED, this.onPasued.bind(this));
|
||||
impl.setEventListener(EventType.STOPPED, this.onStopped.bind(this));
|
||||
impl.setEventListener(EventType.COMPLETED, this.onCompleted.bind(this));
|
||||
impl.setEventListener(EventType.META_LOADED, this.onMetaLoaded.bind(this));
|
||||
impl.setEventListener(EventType.CLICKED, this.onClicked.bind(this));
|
||||
impl.setEventListener(EventType.READY_TO_PLAY, this.onReadyToPlay.bind(this));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
onReadyToPlay: function () {
|
||||
onEnable () {
|
||||
if (this._impl) {
|
||||
this._impl.enable();
|
||||
}
|
||||
},
|
||||
|
||||
onDisable () {
|
||||
if (this._impl) {
|
||||
this._impl.disable();
|
||||
}
|
||||
},
|
||||
|
||||
onDestroy () {
|
||||
if (this._impl) {
|
||||
this._impl.destroy();
|
||||
this._impl = null;
|
||||
}
|
||||
},
|
||||
|
||||
update (dt) {
|
||||
if (this._impl) {
|
||||
this._impl.updateMatrix(this.node);
|
||||
}
|
||||
},
|
||||
|
||||
onReadyToPlay () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.READY_TO_PLAY);
|
||||
this.node.emit('ready-to-play', this);
|
||||
},
|
||||
|
||||
onMetaLoaded: function () {
|
||||
onMetaLoaded () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.META_LOADED);
|
||||
this.node.emit('meta-loaded', this);
|
||||
},
|
||||
|
||||
onClicked: function () {
|
||||
onClicked () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.CLICKED);
|
||||
this.node.emit('clicked', this);
|
||||
},
|
||||
|
||||
onPlaying: function () {
|
||||
onPlaying () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.PLAYING);
|
||||
this.node.emit('playing', this);
|
||||
},
|
||||
|
||||
onPasued: function () {
|
||||
onPasued () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.PAUSED);
|
||||
this.node.emit('paused', this);
|
||||
},
|
||||
|
||||
onStopped: function () {
|
||||
onStopped () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.STOPPED);
|
||||
this.node.emit('stopped', this);
|
||||
},
|
||||
|
||||
onCompleted: function () {
|
||||
onCompleted () {
|
||||
cc.Component.EventHandler.emitEvents(this.videoPlayerEvent, this, EventType.COMPLETED);
|
||||
this.node.emit('completed', this);
|
||||
},
|
||||
@@ -374,10 +382,10 @@ var VideoPlayer = cc.Class({
|
||||
* !#zh 如果视频被暂停播放了,调用这个接口可以继续播放。如果视频被停止播放了,调用这个接口可以从头开始播放。
|
||||
* @method play
|
||||
*/
|
||||
play: function () {
|
||||
if (this._sgNode) {
|
||||
play () {
|
||||
if (this._impl) {
|
||||
this._syncVolume();
|
||||
this._sgNode.play();
|
||||
this._impl.play();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -386,10 +394,10 @@ var VideoPlayer = cc.Class({
|
||||
* !#zh 如果一个视频播放被暂停播放了,调用这个接口可以继续播放。
|
||||
* @method resume
|
||||
*/
|
||||
resume: function () {
|
||||
if (this._sgNode) {
|
||||
resume () {
|
||||
if (this._impl) {
|
||||
this._syncVolume();
|
||||
this._sgNode.resume();
|
||||
this._impl.resume();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -398,9 +406,9 @@ var VideoPlayer = cc.Class({
|
||||
* !#zh 如果一个视频正在播放,调用这个接口可以暂停播放。
|
||||
* @method pause
|
||||
*/
|
||||
pause: function () {
|
||||
if (this._sgNode) {
|
||||
this._sgNode.pause();
|
||||
pause () {
|
||||
if (this._impl) {
|
||||
this._impl.pause();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -409,9 +417,9 @@ var VideoPlayer = cc.Class({
|
||||
* !#zh 如果一个视频正在播放,调用这个接口可以立马停止播放。
|
||||
* @method stop
|
||||
*/
|
||||
stop: function () {
|
||||
if (this._sgNode) {
|
||||
this._sgNode.stop();
|
||||
stop () {
|
||||
if (this._impl) {
|
||||
this._impl.stop();
|
||||
}
|
||||
},
|
||||
|
||||
@@ -421,9 +429,9 @@ var VideoPlayer = cc.Class({
|
||||
* @method getDuration
|
||||
* @returns {Number}
|
||||
*/
|
||||
getDuration: function () {
|
||||
if (this._sgNode) {
|
||||
return this._sgNode.duration();
|
||||
getDuration () {
|
||||
if (this._impl) {
|
||||
return this._impl.duration();
|
||||
}
|
||||
return -1;
|
||||
},
|
||||
@@ -434,9 +442,9 @@ var VideoPlayer = cc.Class({
|
||||
* @method isPlaying
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
isPlaying: function () {
|
||||
if (this._sgNode) {
|
||||
return this._sgNode.isPlaying();
|
||||
isPlaying () {
|
||||
if (this._impl) {
|
||||
return this._impl.isPlaying();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ var components = [
|
||||
require('./CCSlider'),
|
||||
require('./CCLayout'),
|
||||
require('./editbox/CCEditBox'),
|
||||
// require('./CCVideoPlayer'),
|
||||
require('./CCVideoPlayer'),
|
||||
// require('./CCWebView'),
|
||||
require('./CCLabelOutline'),
|
||||
require('./CCRichText'),
|
||||
|
||||
+392
-433
@@ -19,81 +19,277 @@
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
var Utils = require('../platform/utils');
|
||||
var sys = require('../platform/CCSys');
|
||||
const Utils = require('../platform/utils');
|
||||
const sys = require('../platform/CCSys');
|
||||
|
||||
/**
|
||||
* @class
|
||||
* @extends _ccsg.Node
|
||||
* @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
|
||||
*/
|
||||
_ccsg.VideoPlayer = _ccsg.Node.extend(/** @lends _ccsg.VideoPlayer# */{
|
||||
let VideoPlayerImpl = cc.Class({
|
||||
name: 'VideoPlayerImpl',
|
||||
|
||||
ctor: function () {
|
||||
_ccsg.Node.prototype.ctor.call(this);
|
||||
ctor () {
|
||||
// 播放结束等事件处理的队列
|
||||
this._EventList = {};
|
||||
|
||||
this._video = null;
|
||||
this._url = '';
|
||||
|
||||
this._loaded = false;
|
||||
this._visible = false;
|
||||
// 有一些浏览器第一次播放视频需要特殊处理,这个标记用来标识是否播放过
|
||||
this._played = false;
|
||||
this._playing = false;
|
||||
this._ignorePause = false;
|
||||
|
||||
// update matrix cache
|
||||
this._forceUpdate = true;
|
||||
this._m00 = 0;
|
||||
this._m01 = 0;
|
||||
this._m04 = 0;
|
||||
this._m05 = 0;
|
||||
this._m12 = 0;
|
||||
this._m13 = 0;
|
||||
this._w = 0;
|
||||
this._h = 0;
|
||||
},
|
||||
|
||||
_createRenderCmd: function(){
|
||||
return new _ccsg.VideoPlayer.RenderCmd(this);
|
||||
_bindEvent () {
|
||||
var video = this._video, self = this;
|
||||
//binding event
|
||||
video.onloadedmetadata = function () {
|
||||
self._dispatchEvent(VideoPlayerImpl.EventType.META_LOADED);
|
||||
};
|
||||
video.addEventListener("ended", function () {
|
||||
if (self._video !== video) return;
|
||||
self._playing = false;
|
||||
self._dispatchEvent(VideoPlayerImpl.EventType.COMPLETED);
|
||||
});
|
||||
video.addEventListener("play", function () {
|
||||
if (self._video !== video) return;
|
||||
self._dispatchEvent(VideoPlayerImpl.EventType.PLAYING);
|
||||
});
|
||||
video.addEventListener("pause", function () {
|
||||
if (self._ignorePause || self._video !== video) return;
|
||||
self._dispatchEvent(VideoPlayerImpl.EventType.PAUSED);
|
||||
});
|
||||
video.addEventListener("click", function () {
|
||||
self._dispatchEvent(VideoPlayerImpl.EventType.CLICKED);
|
||||
});
|
||||
},
|
||||
|
||||
setURL: function (url) {
|
||||
this._renderCmd.updateURL(url);
|
||||
_updateVisibility () {
|
||||
if (!this._video) return;
|
||||
var video = this._video;
|
||||
if (this._visible) {
|
||||
video.style.visibility = 'visible';
|
||||
}
|
||||
else {
|
||||
video.style.visibility = 'hidden';
|
||||
video.pause();
|
||||
this._playing = false;
|
||||
}
|
||||
this._forceUpdate = true;
|
||||
},
|
||||
|
||||
getURL: function() {
|
||||
return this._renderCmd._url;
|
||||
_updateSize (width, height) {
|
||||
var video = this._video;
|
||||
if (!video) return;
|
||||
|
||||
video.style['width'] = width + 'px';
|
||||
video.style['height'] = height + 'px';
|
||||
},
|
||||
|
||||
play: function () {
|
||||
this._renderCmd.play();
|
||||
},
|
||||
|
||||
pause: function () {
|
||||
this._renderCmd.pause();
|
||||
},
|
||||
|
||||
_resume: function () {
|
||||
this._renderCmd.play();
|
||||
},
|
||||
|
||||
stop: function () {
|
||||
this._renderCmd.stop();
|
||||
},
|
||||
|
||||
setVolume: function (volume) {
|
||||
this._renderCmd.setVolume(volume);
|
||||
},
|
||||
|
||||
seekTo: function (time) {
|
||||
this._renderCmd.seekTo(time);
|
||||
},
|
||||
|
||||
isPlaying: function () {
|
||||
return this._renderCmd.isPlaying();
|
||||
},
|
||||
|
||||
duration: function () {
|
||||
return this._renderCmd.duration();
|
||||
},
|
||||
|
||||
currentTime: function() {
|
||||
return this._renderCmd.currentTime();
|
||||
_createDom () {
|
||||
var video = document.createElement('video');
|
||||
video.style.position = "absolute";
|
||||
video.style.bottom = "0px";
|
||||
video.style.left = "0px";
|
||||
video.className = "cocosVideo";
|
||||
video.setAttribute('preload', true);
|
||||
video.setAttribute('webkit-playsinline', '');
|
||||
video.setAttribute('playsinline', '');
|
||||
this._video = video;
|
||||
cc.game.container.appendChild(video);
|
||||
},
|
||||
|
||||
createDomElementIfNeeded: function () {
|
||||
if (!this._renderCmd._video) {
|
||||
this._renderCmd.createDom();
|
||||
if (!this._video) {
|
||||
this._createDom();
|
||||
}
|
||||
},
|
||||
|
||||
removeDom () {
|
||||
var video = this._video;
|
||||
if (video) {
|
||||
var hasChild = Utils.contains(cc.game.container, video);
|
||||
if (hasChild)
|
||||
cc.game.container.removeChild(video);
|
||||
}
|
||||
this._video = null;
|
||||
this._url = "";
|
||||
},
|
||||
|
||||
setURL (path) {
|
||||
var source, video, extname;
|
||||
|
||||
if (this._url === path) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._url = path;
|
||||
|
||||
if (cc.loader.resPath && !/^http/.test(path))
|
||||
path = cc.path.join(cc.loader.resPath, path);
|
||||
|
||||
this.removeDom();
|
||||
this.createDomElementIfNeeded();
|
||||
|
||||
this._bindEvent();
|
||||
|
||||
video = this._video;
|
||||
|
||||
video.oncanplay = function () {
|
||||
if (this._loaded)
|
||||
return;
|
||||
this._loaded = true;
|
||||
// node.setContentSize(node._contentSize.width, node._contentSize.height);
|
||||
video.currentTime = 0;
|
||||
this._dispatchEvent(VideoPlayerImpl.EventType.READY_TO_PLAY);
|
||||
this._updateVisibility();
|
||||
}.bind(this);
|
||||
|
||||
video.style["visibility"] = "hidden";
|
||||
this._loaded = false;
|
||||
this._played = false;
|
||||
this._playing = false;
|
||||
|
||||
source = document.createElement("source");
|
||||
source.src = path;
|
||||
video.appendChild(source);
|
||||
|
||||
extname = cc.path.extname(path);
|
||||
var polyfill = VideoPlayerImpl._polyfill;
|
||||
for (var i = 0; i < polyfill.canPlayType.length; i++) {
|
||||
if (extname !== polyfill.canPlayType[i]) {
|
||||
source = document.createElement("source");
|
||||
source.src = path.replace(extname, polyfill.canPlayType[i]);
|
||||
video.appendChild(source);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
getURL: function() {
|
||||
return this._url;
|
||||
},
|
||||
|
||||
play: function () {
|
||||
var video = this._video;
|
||||
if (!video || !this._visible) return;
|
||||
|
||||
this._played = true;
|
||||
if (this._playing) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (VideoPlayerImpl._polyfill.autoplayAfterOperation) {
|
||||
var self = this;
|
||||
setTimeout(function () {
|
||||
video.play();
|
||||
self._playing = true;
|
||||
}, 20);
|
||||
}
|
||||
else {
|
||||
video.play();
|
||||
this._playing = true;
|
||||
}
|
||||
},
|
||||
|
||||
pause: function () {
|
||||
var video = this._video;
|
||||
if (!this._playing) return;
|
||||
|
||||
this._playing = false;
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
video.pause();
|
||||
},
|
||||
|
||||
_resume: function () {
|
||||
this.play();
|
||||
},
|
||||
|
||||
stop: function () {
|
||||
var video = this._video;
|
||||
if (!video || !this._visible) return;
|
||||
this._ignorePause = true;
|
||||
video.pause();
|
||||
var self = this;
|
||||
setTimeout(function () {
|
||||
self._dispatchEvent(VideoPlayerImpl.EventType.STOPPED);
|
||||
self._ignorePause = false;
|
||||
}, 0);
|
||||
// 恢复到视频起始位置
|
||||
video.currentTime = 0;
|
||||
this._playing = false;
|
||||
},
|
||||
|
||||
setVolume: function (volume) {
|
||||
var video = this._video;
|
||||
if (video) {
|
||||
video.volume = volume;
|
||||
}
|
||||
},
|
||||
|
||||
seekTo: function (time) {
|
||||
var video = this._video;
|
||||
if (!video) return;
|
||||
|
||||
if (this._loaded) {
|
||||
video.currentTime = time;
|
||||
}
|
||||
else {
|
||||
var cb = function () {
|
||||
video.currentTime = time;
|
||||
video.removeEventListener(VideoPlayerImpl._polyfill.event, cb);
|
||||
};
|
||||
video.addEventListener(VideoPlayerImpl._polyfill.event, cb);
|
||||
}
|
||||
if (VideoPlayerImpl._polyfill.autoplayAfterOperation && this.isPlaying()) {
|
||||
setTimeout(function () {
|
||||
video.play();
|
||||
}, 20);
|
||||
}
|
||||
},
|
||||
|
||||
isPlaying: function () {
|
||||
var video = this._video;
|
||||
if (VideoPlayerImpl._polyfill.autoplayAfterOperation && this._playing) {
|
||||
setTimeout(function () {
|
||||
video.play();
|
||||
}, 20);
|
||||
}
|
||||
return this._playing;
|
||||
},
|
||||
|
||||
duration: function () {
|
||||
var video = this._video;
|
||||
var duration = -1;
|
||||
if (!video) return duration;
|
||||
|
||||
duration = video.duration;
|
||||
if (duration <= 0) {
|
||||
cc.logID(7702);
|
||||
}
|
||||
|
||||
return duration;
|
||||
},
|
||||
|
||||
currentTime: function() {
|
||||
var video = this._video;
|
||||
if (!video) return -1;
|
||||
|
||||
return video.currentTime;
|
||||
},
|
||||
|
||||
setKeepAspectRatioEnabled: function () {
|
||||
cc.logID(7700);
|
||||
},
|
||||
@@ -103,10 +299,10 @@ _ccsg.VideoPlayer = _ccsg.Node.extend(/** @lends _ccsg.VideoPlayer# */{
|
||||
},
|
||||
|
||||
setFullScreenEnabled: function (enable) {
|
||||
var video = this._renderCmd._video;
|
||||
var video = this._video;
|
||||
if (!video) return;
|
||||
|
||||
if(enable)
|
||||
if (enable)
|
||||
cc.screen.requestFullScreen(video);
|
||||
else
|
||||
cc.screen.exitFullScreen(video);
|
||||
@@ -128,75 +324,98 @@ _ccsg.VideoPlayer = _ccsg.Node.extend(/** @lends _ccsg.VideoPlayer# */{
|
||||
_dispatchEvent: function (event) {
|
||||
var callback = this._EventList[event];
|
||||
if (callback)
|
||||
callback.call(this, this, this._renderCmd._video.src);
|
||||
callback.call(this, this, this._video.src);
|
||||
},
|
||||
|
||||
onPlayEvent: function () {
|
||||
var callback = this._EventList[_ccsg.VideoPlayer.EventType.PLAYING];
|
||||
callback.call(this, this, this._renderCmd._video.src);
|
||||
var callback = this._EventList[VideoPlayerImpl.EventType.PLAYING];
|
||||
callback.call(this, this, this._video.src);
|
||||
},
|
||||
|
||||
setContentSize: function (width, height) {
|
||||
if (width.width !== undefined && width.height !== undefined) {
|
||||
height = width.height;
|
||||
width = width.width;
|
||||
}
|
||||
_ccsg.Node.prototype.setContentSize.call(this, width, height);
|
||||
this._renderCmd.updateSize(width, height);
|
||||
},
|
||||
|
||||
onEnter: function () {
|
||||
_ccsg.Node.prototype.onEnter.call(this);
|
||||
var list = _ccsg.VideoPlayer.elements;
|
||||
if(list.indexOf(this) === -1)
|
||||
enable: function () {
|
||||
var list = VideoPlayerImpl.elements;
|
||||
if (list.indexOf(this) === -1)
|
||||
list.push(this);
|
||||
this.setVisible(true);
|
||||
},
|
||||
|
||||
cleanup: function () {
|
||||
this._super();
|
||||
this._renderCmd.removeDom();
|
||||
},
|
||||
|
||||
onExit: function () {
|
||||
_ccsg.Node.prototype.onExit.call(this);
|
||||
var list = _ccsg.VideoPlayer.elements;
|
||||
disable: function () {
|
||||
var list = VideoPlayerImpl.elements;
|
||||
var index = list.indexOf(this);
|
||||
if(index !== -1)
|
||||
if (index !== -1)
|
||||
list.splice(index, 1);
|
||||
this.setVisible(false);
|
||||
},
|
||||
|
||||
setVisible: function ( visible ) {
|
||||
_ccsg.Node.prototype.setVisible.call(this, visible);
|
||||
this._renderCmd.updateVisibility();
|
||||
}
|
||||
});
|
||||
destroy: function () {
|
||||
this.disable();
|
||||
this.removeDom();
|
||||
},
|
||||
|
||||
// video 队列,所有 vidoe 在 onEnter 的时候都会插入这个队列
|
||||
_ccsg.VideoPlayer.elements = [];
|
||||
// video 在 game_hide 事件中被自动暂停的队列,用于回复的时候重新开始播放
|
||||
_ccsg.VideoPlayer.pauseElements = [];
|
||||
|
||||
cc.game.on(cc.game.EVENT_HIDE, function () {
|
||||
var list = _ccsg.VideoPlayer.elements;
|
||||
for (var element, i = 0; i < list.length; i++) {
|
||||
element = list[i];
|
||||
if (element.isPlaying()) {
|
||||
element.pause();
|
||||
_ccsg.VideoPlayer.pauseElements.push(element);
|
||||
setVisible: function (visible) {
|
||||
if (this._visible !== visible) {
|
||||
this._visible = !!visible;
|
||||
this._updateVisibility();
|
||||
}
|
||||
},
|
||||
|
||||
updateMatrix (node) {
|
||||
if (!this._video || !this._visible) return;
|
||||
|
||||
var mat = node._worldMatrix;
|
||||
if (!this._forceUpdate &&
|
||||
this._m00 === mat.m00 && this._m01 === mat.m01 && this._m04 === mat.m04 && this._m05 === mat.m05 && this._m12 === mat.m12 && this._m13 === mat.m13 &&
|
||||
this._w === node._contentSize.width && this._h === node._contentSize.height) {
|
||||
return;
|
||||
}
|
||||
|
||||
// update matrix cache
|
||||
this._m00 = mat.m00;
|
||||
this._m01 = mat.m01;
|
||||
this._m04 = mat.m04;
|
||||
this._m05 = mat.m05;
|
||||
this._m12 = mat.m12;
|
||||
this._m13 = mat.m13;
|
||||
this._w = node._contentSize.width;
|
||||
this._h = node._contentSize.height;
|
||||
|
||||
var scaleX = cc.view._scaleX, scaleY = cc.view._scaleY;
|
||||
var dpr = cc.view._devicePixelRatio;
|
||||
|
||||
scaleX /= dpr;
|
||||
scaleY /= dpr;
|
||||
|
||||
var container = cc.game.container;
|
||||
var a = mat.m00 * scaleX, b = mat.m01, c = mat.m04, d = mat.m05 * scaleY;
|
||||
|
||||
var offsetX = container && container.style.paddingLeft && parseInt(container.style.paddingLeft);
|
||||
var offsetY = container && container.style.paddingBottom && parseInt(container.style.paddingBottom);
|
||||
var w, h;
|
||||
if (VideoPlayerImpl._polyfill.zoomInvalid) {
|
||||
this._updateSize(this._w * a, this._h * d);
|
||||
a = 1;
|
||||
d = 1;
|
||||
w = this._w * scaleX;
|
||||
h = this._h * scaleY;
|
||||
}
|
||||
else {
|
||||
w = this._video.clientWidth / scaleX;
|
||||
h = this._video.clientHeight / scaleY;
|
||||
}
|
||||
|
||||
var appx = w * node._anchorPoint.x;
|
||||
var appy = h - h * node._anchorPoint.y;
|
||||
var tx = mat.m12 * scaleX - appx + offsetX, ty = mat.m13 * scaleY - appy + offsetY;
|
||||
|
||||
var matrix = "matrix(" + a + "," + -b + "," + -c + "," + d + "," + tx + "," + -ty + ")";
|
||||
this._video.style['transform'] = matrix;
|
||||
this._video.style['-webkit-transform'] = matrix;
|
||||
this._video.style['transform-origin'] = '0px 100% 0px';
|
||||
this._video.style['-webkit-transform-origin'] = '0px 100% 0px';
|
||||
}
|
||||
});
|
||||
|
||||
cc.game.on(cc.game.EVENT_SHOW, function () {
|
||||
var list = _ccsg.VideoPlayer.pauseElements;
|
||||
var element = list.pop();
|
||||
while (element) {
|
||||
element.play();
|
||||
element = list.pop();
|
||||
}
|
||||
});
|
||||
|
||||
_ccsg.VideoPlayer.EventType = {
|
||||
VideoPlayerImpl.EventType = {
|
||||
PLAYING: 0,
|
||||
PAUSED: 1,
|
||||
STOPPED: 2,
|
||||
@@ -206,337 +425,77 @@ _ccsg.VideoPlayer.EventType = {
|
||||
READY_TO_PLAY: 6
|
||||
};
|
||||
|
||||
(function (video) {
|
||||
/**
|
||||
* Adapter various machines
|
||||
* @devicePixelRatio Whether you need to consider devicePixelRatio calculated position
|
||||
* @event To get the data using events
|
||||
*/
|
||||
video._polyfill = {
|
||||
devicePixelRatio: false,
|
||||
event: "canplay",
|
||||
canPlayType: []
|
||||
};
|
||||
// video 队列,所有 vidoe 在 onEnter 的时候都会插入这个队列
|
||||
VideoPlayerImpl.elements = [];
|
||||
// video 在 game_hide 事件中被自动暂停的队列,用于回复的时候重新开始播放
|
||||
VideoPlayerImpl.pauseElements = [];
|
||||
|
||||
(function () {
|
||||
/**
|
||||
* Some old browser only supports Theora encode video
|
||||
* But native does not support this encode,
|
||||
* so it is best to provide mp4 and webm or ogv file
|
||||
*/
|
||||
var dom = document.createElement("video");
|
||||
if (sys.platform !== sys.WECHAT_GAME) {
|
||||
if (dom.canPlayType("video/ogg")) {
|
||||
video._polyfill.canPlayType.push(".ogg");
|
||||
video._polyfill.canPlayType.push(".ogv");
|
||||
}
|
||||
if (dom.canPlayType("video/mp4"))
|
||||
video._polyfill.canPlayType.push(".mp4");
|
||||
if (dom.canPlayType("video/webm"))
|
||||
video._polyfill.canPlayType.push(".webm");
|
||||
cc.game.on(cc.game.EVENT_HIDE, function () {
|
||||
var list = VideoPlayerImpl.elements;
|
||||
for (var element, i = 0; i < list.length; i++) {
|
||||
element = list[i];
|
||||
if (element.isPlaying()) {
|
||||
element.pause();
|
||||
VideoPlayerImpl.pauseElements.push(element);
|
||||
}
|
||||
})();
|
||||
|
||||
if (cc.sys.browserType === cc.sys.BROWSER_TYPE_FIREFOX) {
|
||||
video._polyfill.autoplayAfterOperation = true;
|
||||
}
|
||||
});
|
||||
|
||||
if (
|
||||
cc.sys.OS_ANDROID === cc.sys.os && (
|
||||
cc.sys.browserType === cc.sys.BROWSER_TYPE_SOUGOU ||
|
||||
cc.sys.browserType === cc.sys.BROWSER_TYPE_360
|
||||
)
|
||||
) {
|
||||
video._polyfill.zoomInvalid = true;
|
||||
cc.game.on(cc.game.EVENT_SHOW, function () {
|
||||
var list = VideoPlayerImpl.pauseElements;
|
||||
var element = list.pop();
|
||||
while (element) {
|
||||
element.play();
|
||||
element = list.pop();
|
||||
}
|
||||
});
|
||||
|
||||
var style = document.createElement("style");
|
||||
style.innerHTML = ".cocosVideo:-moz-full-screen{transform:matrix(1,0,0,1,0,0) !important;}" +
|
||||
".cocosVideo:full-screen{transform:matrix(1,0,0,1,0,0) !important;}" +
|
||||
".cocosVideo:-webkit-full-screen{transform:matrix(1,0,0,1,0,0) !important;}";
|
||||
document.head.appendChild(style);
|
||||
|
||||
})(_ccsg.VideoPlayer);
|
||||
/**
|
||||
* Adapter various machines
|
||||
* @devicePixelRatio Whether you need to consider devicePixelRatio calculated position
|
||||
* @event To get the data using events
|
||||
*/
|
||||
VideoPlayerImpl._polyfill = {
|
||||
devicePixelRatio: false,
|
||||
event: "canplay",
|
||||
canPlayType: []
|
||||
};
|
||||
|
||||
(function (polyfill) {
|
||||
/**
|
||||
* Some old browser only supports Theora encode video
|
||||
* But native does not support this encode,
|
||||
* so it is best to provide mp4 and webm or ogv file
|
||||
*/
|
||||
let dom = document.createElement("video");
|
||||
if (sys.platform !== sys.WECHAT_GAME) {
|
||||
if (dom.canPlayType("video/ogg")) {
|
||||
VideoPlayerImpl._polyfill.canPlayType.push(".ogg");
|
||||
VideoPlayerImpl._polyfill.canPlayType.push(".ogv");
|
||||
}
|
||||
if (dom.canPlayType("video/mp4"))
|
||||
VideoPlayerImpl._polyfill.canPlayType.push(".mp4");
|
||||
if (dom.canPlayType("video/webm"))
|
||||
VideoPlayerImpl._polyfill.canPlayType.push(".webm");
|
||||
}
|
||||
|
||||
_ccsg.VideoPlayer.RenderCmd = function (node) {
|
||||
this._rootCtor(node);
|
||||
if (sys.browserType === sys.BROWSER_TYPE_FIREFOX) {
|
||||
VideoPlayerImpl._polyfill.autoplayAfterOperation = true;
|
||||
}
|
||||
|
||||
this._video = null;
|
||||
this._url = '';
|
||||
if (
|
||||
sys.OS_ANDROID === sys.os && (
|
||||
sys.browserType === sys.BROWSER_TYPE_SOUGOU ||
|
||||
sys.browserType === sys.BROWSER_TYPE_360
|
||||
)
|
||||
) {
|
||||
VideoPlayerImpl._polyfill.zoomInvalid = true;
|
||||
}
|
||||
|
||||
// 有一些浏览器第一次播放视频需要特殊处理,这个标记用来标识是否播放过
|
||||
this._played = false;
|
||||
this._playing = false;
|
||||
this._ignorePause = false;
|
||||
};
|
||||
let style = document.createElement("style");
|
||||
style.innerHTML = ".cocosVideo:-moz-full-screen{transform:matrix(1,0,0,1,0,0) !important;}" +
|
||||
".cocosVideo:full-screen{transform:matrix(1,0,0,1,0,0) !important;}" +
|
||||
".cocosVideo:-webkit-full-screen{transform:matrix(1,0,0,1,0,0) !important;}";
|
||||
document.head.appendChild(style);
|
||||
|
||||
var proto = _ccsg.VideoPlayer.RenderCmd.prototype = Object.create(_ccsg.Node.CanvasRenderCmd.prototype);
|
||||
proto.constructor = _ccsg.VideoPlayer.RenderCmd;
|
||||
|
||||
proto.transform = function (parentCmd, recursive) {
|
||||
this.originTransform(parentCmd, recursive);
|
||||
this.updateMatrix();
|
||||
};
|
||||
|
||||
proto.updateMatrix = function () {
|
||||
if (!this._video) return;
|
||||
var node = this._node, scaleX = cc.view._scaleX, scaleY = cc.view._scaleY;
|
||||
var dpr = cc.view._devicePixelRatio;
|
||||
var t = this._worldTransform;
|
||||
|
||||
scaleX /= dpr;
|
||||
scaleY /= dpr;
|
||||
|
||||
var container = cc.game.container;
|
||||
var a = t.a * scaleX, b = t.b, c = t.c, d = t.d * scaleY;
|
||||
|
||||
var offsetX = container && container.style.paddingLeft && parseInt(container.style.paddingLeft);
|
||||
var offsetY = container && container.style.paddingBottom && parseInt(container.style.paddingBottom);
|
||||
var tx = t.tx * scaleX + offsetX, ty = t.ty * scaleY + offsetY;
|
||||
|
||||
if (polyfill.zoomInvalid) {
|
||||
this.updateSize(node._contentSize.width * a, node._contentSize.height * d);
|
||||
a = 1;
|
||||
d = 1;
|
||||
}
|
||||
|
||||
var matrix = "matrix(" + a + "," + -b + "," + -c + "," + d + "," + tx + "," + -ty + ")";
|
||||
this._video.style['transform'] = matrix;
|
||||
this._video.style['-webkit-transform'] = matrix;
|
||||
this._video.style['transform-origin'] = '0px 100% 0px';
|
||||
this._video.style['-webkit-transform-origin'] = '0px 100% 0px';
|
||||
};
|
||||
|
||||
proto.updateURL = function (path) {
|
||||
var source, video, extname;
|
||||
var node = this._node;
|
||||
|
||||
if (this._url === path) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._url = path;
|
||||
|
||||
if (cc.loader.resPath && !/^http/.test(path))
|
||||
path = cc.path.join(cc.loader.resPath, path);
|
||||
|
||||
this.removeDom();
|
||||
this.createDom();
|
||||
|
||||
this.bindEvent();
|
||||
|
||||
video = this._video;
|
||||
|
||||
video.oncanplay = function () {
|
||||
if (this._loaded)
|
||||
return;
|
||||
this._loaded = true;
|
||||
node.setContentSize(node._contentSize.width, node._contentSize.height);
|
||||
video.currentTime = 0;
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.READY_TO_PLAY);
|
||||
this.updateVisibility();
|
||||
this.updateMatrix();
|
||||
}.bind(this);
|
||||
|
||||
//video.controls = "controls";
|
||||
// if preload set to metadata, the canplay event can't be fired on safari
|
||||
// video.preload = "metadata";
|
||||
video.style["visibility"] = "hidden";
|
||||
this._loaded = false;
|
||||
this._played = false;
|
||||
this._playing = false;
|
||||
|
||||
source = document.createElement("source");
|
||||
source.src = path;
|
||||
video.appendChild(source);
|
||||
|
||||
extname = cc.path.extname(path);
|
||||
for (var i = 0; i < polyfill.canPlayType.length; i++) {
|
||||
if (extname !== polyfill.canPlayType[i]) {
|
||||
source = document.createElement("source");
|
||||
source.src = path.replace(extname, polyfill.canPlayType[i]);
|
||||
video.appendChild(source);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
proto.bindEvent = function () {
|
||||
var node = this._node, video = this._video, self = this;
|
||||
//binding event
|
||||
video.onloadedmetadata = function () {
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.META_LOADED);
|
||||
};
|
||||
video.addEventListener("ended", function () {
|
||||
if (self._video !== video) return;
|
||||
this._playing = false;
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.COMPLETED);
|
||||
}.bind(this));
|
||||
video.addEventListener("play", function () {
|
||||
if (self._video !== video) return;
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.PLAYING);
|
||||
});
|
||||
video.addEventListener("pause", function () {
|
||||
if (self._ignorePause || self._video !== video) return;
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.PAUSED);
|
||||
});
|
||||
video.addEventListener("click", function () {
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.CLICKED);
|
||||
});
|
||||
};
|
||||
|
||||
proto.updateVisibility = function () {
|
||||
var node = this._node;
|
||||
if (!this._video) return;
|
||||
var video = this._video;
|
||||
if (node.visible) {
|
||||
video.style.visibility = 'visible';
|
||||
}
|
||||
else {
|
||||
video.style.visibility = 'hidden';
|
||||
video.pause();
|
||||
this._playing = false;
|
||||
}
|
||||
};
|
||||
|
||||
proto.createDom = function () {
|
||||
var video = document.createElement('video');
|
||||
video.style.position = "absolute";
|
||||
video.style.bottom = "0px";
|
||||
video.style.left = "0px";
|
||||
video.className = "cocosVideo";
|
||||
video.setAttribute('preload', true);
|
||||
video.setAttribute('webkit-playsinline', '');
|
||||
video.setAttribute('playsinline', '');
|
||||
this._video = video;
|
||||
cc.game.container.appendChild(video);
|
||||
};
|
||||
|
||||
proto.removeDom = function () {
|
||||
var video = this._video;
|
||||
if (video) {
|
||||
var hasChild = Utils.contains(cc.game.container, video);
|
||||
if (hasChild)
|
||||
cc.game.container.removeChild(video);
|
||||
}
|
||||
this._video = null;
|
||||
this._url = "";
|
||||
};
|
||||
|
||||
proto.updateSize = function (width, height) {
|
||||
var video = this._video;
|
||||
if (!video) return;
|
||||
|
||||
video.style['width'] = width + 'px';
|
||||
video.style['height'] = height + 'px';
|
||||
};
|
||||
|
||||
// 播放控制
|
||||
proto.play = function () {
|
||||
var video = this._video;
|
||||
if (!video || !this._node.isVisible()) return;
|
||||
|
||||
this._played = true;
|
||||
if (this._playing) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_ccsg.VideoPlayer._polyfill.autoplayAfterOperation) {
|
||||
var self = this;
|
||||
setTimeout(function () {
|
||||
video.play();
|
||||
self._playing = true;
|
||||
}, 20);
|
||||
}
|
||||
else {
|
||||
video.play();
|
||||
this._playing = true;
|
||||
}
|
||||
};
|
||||
|
||||
proto.pause = function () {
|
||||
var video = this._video;
|
||||
if (!this._playing) return;
|
||||
|
||||
this._playing = false;
|
||||
if (!video) {
|
||||
return;
|
||||
}
|
||||
video.pause();
|
||||
};
|
||||
|
||||
proto.stop = function () {
|
||||
var video = this._video;
|
||||
if (!video || !this._node.isVisible()) return;
|
||||
this._ignorePause = true;
|
||||
video.pause();
|
||||
var node = this._node;
|
||||
setTimeout(function () {
|
||||
node._dispatchEvent(_ccsg.VideoPlayer.EventType.STOPPED);
|
||||
this._ignorePause = false;
|
||||
}.bind(this), 0);
|
||||
// 恢复到视频起始位置
|
||||
video.currentTime = 0;
|
||||
this._playing = false;
|
||||
};
|
||||
|
||||
proto.seekTo = function (sec) {
|
||||
var video = this._video;
|
||||
if (!video) return;
|
||||
|
||||
if (this._loaded) {
|
||||
video.currentTime = sec;
|
||||
}
|
||||
else {
|
||||
var cb = function () {
|
||||
video.currentTime = sec;
|
||||
video.removeEventListener(polyfill.event, cb);
|
||||
};
|
||||
video.addEventListener(polyfill.event, cb);
|
||||
}
|
||||
if (_ccsg.VideoPlayer._polyfill.autoplayAfterOperation && this.isPlaying()) {
|
||||
setTimeout(function () {
|
||||
video.play();
|
||||
}, 20);
|
||||
}
|
||||
};
|
||||
|
||||
proto.setVolume = function (volume) {
|
||||
var video = this._video;
|
||||
if (video) {
|
||||
video.volume = volume;
|
||||
}
|
||||
};
|
||||
|
||||
proto.isPlaying = function () {
|
||||
var video = this._video;
|
||||
if (_ccsg.VideoPlayer._polyfill.autoplayAfterOperation && this._playing) {
|
||||
setTimeout(function () {
|
||||
video.play();
|
||||
}, 20);
|
||||
}
|
||||
return this._playing;
|
||||
};
|
||||
|
||||
proto.duration = function () {
|
||||
var video = this._video;
|
||||
var duration = -1;
|
||||
if (!video) return duration;
|
||||
|
||||
duration = video.duration;
|
||||
if (duration <= 0) {
|
||||
cc.logID(7702);
|
||||
}
|
||||
|
||||
return duration;
|
||||
};
|
||||
|
||||
proto.currentTime = function () {
|
||||
var video = this._video;
|
||||
if (!video) return -1;
|
||||
|
||||
return video.currentTime;
|
||||
};
|
||||
|
||||
})(_ccsg.VideoPlayer._polyfill);
|
||||
module.exports = VideoPlayerImpl;
|
||||
Reference in New Issue
Block a user