diff --git a/cocos2d/videoplayer/CCVideoPlayer.js b/cocos2d/videoplayer/CCVideoPlayer.js index 18ca911967..4e18bbd0cc 100644 --- a/cocos2d/videoplayer/CCVideoPlayer.js +++ b/cocos2d/videoplayer/CCVideoPlayer.js @@ -62,8 +62,8 @@ const VideoPlayerImpl = require('./video-player-impl'); * @property {Number} CLICKED */ /** - * !#en ready to play - * !#zh 视频准备好了,可以开始播放了 + * !#en ready to play, this event is not guaranteed to be triggered on all platform or browser, please don't rely on it to play your video.
+ * !#zh 视频准备好了,这个事件并不保障会在所有平台或浏览器中被触发,它依赖于平台实现,请不要依赖于这个事件做视频播放的控制。 * @property {Number} READY_TO_PLAY */ const EventType = VideoPlayerImpl.EventType; @@ -160,8 +160,6 @@ let VideoPlayer = cc.Class({ return this._clip; }, set: function (value) { - if (typeof value !== 'string') - value = ''; this._clip = value; this._updateVideoSource(); }, diff --git a/cocos2d/videoplayer/video-player-impl.js b/cocos2d/videoplayer/video-player-impl.js index d5b8b9fabb..18d50965ff 100644 --- a/cocos2d/videoplayer/video-player-impl.js +++ b/cocos2d/videoplayer/video-player-impl.js @@ -62,9 +62,9 @@ let VideoPlayerImpl = cc.Class({ _bindEvent () { let video = this._video, self = this; //binding event - video.onloadedmetadata = function () { + video.addEventListener("loadedmetadata", function () { self._dispatchEvent(VideoPlayerImpl.EventType.META_LOADED); - }; + }); video.addEventListener("ended", function () { if (self._video !== video) return; self._playing = false; @@ -113,9 +113,16 @@ let VideoPlayerImpl = cc.Class({ video.style.bottom = "0px"; video.style.left = "0px"; video.className = "cocosVideo"; - video.setAttribute('preload', true); + video.setAttribute('preload', 'auto'); video.setAttribute('webkit-playsinline', ''); video.setAttribute('playsinline', ''); + + // Stupid tencent x5 adaptation + let orientation = cc.winSize.width > cc.winSize.height ? "landscape" : "portrait"; + video.setAttribute("x5-playsinline", ""); + video.setAttribute("x5-video-player-type", "h5"); + video.setAttribute("x5-video-orientation", orientation); + this._video = video; cc.game.container.appendChild(video); }, @@ -138,7 +145,7 @@ let VideoPlayerImpl = cc.Class({ }, setURL (path) { - let source, video, extname; + let source, extname; if (this._url === path) { return; @@ -154,17 +161,24 @@ let VideoPlayerImpl = cc.Class({ this._bindEvent(); - video = this._video; - - video.oncanplay = function () { + function onCanPlay () { 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); + let video = this._video; + if (video.readyState === 4) { + this._loaded = true; + // node.setContentSize(node._contentSize.width, node._contentSize.height); + video.currentTime = 0; + this._dispatchEvent(VideoPlayerImpl.EventType.READY_TO_PLAY); + this._updateVisibility(); + } + } + + let callback = onCanPlay.bind(this); + let video = this._video; + video.addEventListener('canplay', callback); + video.addEventListener('canplaythrough', callback); + video.addEventListener('suspend', callback); video.style["visibility"] = "hidden"; this._loaded = false;