cc.VideoView = class("VideoView"); cc.VideoView.ClassName = "VideoView" function cc.VideoView:saveToXmlNode(xmlNode) cc.Widget.saveToXmlNode(self , xmlNode); xmlNode.FilePath = self.FilePath; end function cc.VideoView:loadFromXmlNode(xmlNode) cc.Widget.loadFromXmlNode(self , xmlNode); self:setFilePath(xmlNode.FilePath); end function cc.VideoView:extend(node) cc.Widget:extend(node); node:setName("VideoView"); node.FilePath = ""; node.VideoNode = cc.Node3D:create(); node.VideoSource = cc.VideoSource:create(); node.VideoNode:addComponent(node.VideoSource); local sprite = cc.Sprite:create(); sprite:setPosition(cc.p(0,0)); sprite:setAnchorPoint(cc.p(0,0)); self.VideoSprite = sprite; node:addProtectedChild(node.VideoNode); node:addProtectedChild(sprite); local function onWidgetEvent(widget , event) -- SIZE_CHANGED if event == 3 then if not node:isAutoSize() then local contentSize = node:getContentSize(); if texture then local texture = node.VideoSource:getTexture(); local size = texture:getContentSize(); node.VideoSprite:setScale(contentSize.width / size.width , contentSize.height / size.height); end else node.VideoSprite:setScale(1,1); end end end node:setWidgetEventListener(onWidgetEvent); node:extendClass(self); end function cc.VideoView:setAutoSize(auto) local texture = self.VideoSource:getTexture(); if texture then local size = texture:getContentSize(); self:setContentSize(size); end cc.Widget.setAutoSize(self , auto); end function cc.VideoView:setFilePath(filePath) self.FilePath = filePath; self.VideoSource:setVideoFile(filePath); self.VideoSource:stop(); self.VideoSource:play(); local texture = self.VideoSource:getTexture(); if texture then self.VideoSprite:setTexture(texture); local rect = cc.rect(0,0,0,0); local size = texture:getContentSize(); rect.width = size.width; rect.height = size.height; self.VideoSprite:setTextureRect(rect); if self:isAutoSize() then self:setContentSize(size); self:setSize(size); self.VideoSprite:setScale(1,1); else local contentSize = self:getContentSize(); self.VideoSprite:setScale(contentSize.width / size.width , contentSize.height / size.height); end end end function cc.VideoView:createNode() local layer = cc.Widget:create(); cc.VideoView:extend(layer); return layer; end -- 设置默认值 function cc.VideoView:setDefaults() -- 默认需要响应touch事件 self:setTouchEnabled(false) self:setAutoSize(true); --self:setSize(cc.size(300,300)); end