2017-03-01 79 views
0

我的要求是在帧中的平面上简单播放视频(json文件中的url)。我在我的HTML创建的视频实体如下动态添加视频在动画中

<a-video id="video_1" position="0 0 2" geometry="width:2.4;height:1.4"></a-video> 

在我的注册组件我已经添加了SRC文件,视频如下

AFRAME.registerComponent('myComp', { 
    schema: { 
     file: {type: 'asset', default: 'assets/data/file1.json'}, 
     var: {type: 'number', default: 0} 
    }, 
    init: function() { 
    }, 
    update: function() { 
     var data = this.data; 
     var scene = this.el.sceneEl; 
     var screen = scene.querySelector('#video_' + data.var); 
     load(data.file, function (response) { 
      var products = response.mydata; 
      screen.setAttribute('src',products[data.var].videoUrl); 
     }); 
     this.el.addEventListener('mouseenter', function() { 
      console.log("mouseenter",screen.getAttribute('src')); 
     }); 
    } 
}); 

我的控制台日志一起显示在JSON文件提到的路径

"mouseenter assets/img/movies/videos/video1.mp4" 

在网络选项卡上,我可以看到我的文件得到了获取与媒体类型和状态的200元。但我仍得到错误

components:texture:warn `$s` is not a valid video +41ms assets/img/movies/videos/video1.mp4 

index.html:1 [.Offscreen-For-WebGL-000000BA313F15D0]RENDER WARNING: texture bound to texture unit 0 is not renderable. It maybe non-power-of-2 and have incompatible texture filtering. 

什么是添加视频的正确方法。我哪里错了。请帮助

+0

@ngokevin,请你帮我解决这个问题 – AishApp

回答

0

https://aframe.io/docs/0.5.0/guides/using-javascript-and-dom-apis.html#creating-an-entity-with-createelement

var videoEl = document.createElement('a-video'); 
videoEl.setAttribute('src', videoUrl); 
this.el.appendChild(videoEl); 
+0

感谢您的回复。我的代码在Firefox中运行良好,但在铬中,视频无法正常工作。前面提到的 – AishApp

+0

,视频只能在firefix中播放,而不能在Chrome中播放。还有,我们可以通过点击按钮来播放和停止视频。 ?请帮忙 – AishApp

0

我有同样的问题,只是设法得到它在Chrome浏览器的东西是这样的:

// Create a new asset 
var new_asset = document.createElement('video'); 
new_asset.setAttribute('id', 'dynVid'); // Create a unique id for asset 
new_asset.setAttribute('src', videoUrl); 

// Append the new video to the a-assets, where a-assets id="assets-id" 
document.getElementById('assets-id').appendChild(new_asset); 

// Add the asset to the a-video 
screen.setAttribute('src', '#dynVid'); 
// Start playback 
new_asset.play(); 

这额外的好处,你可以控制必要时播放(new_asset.pause(),new_asset.currentTime = X,muted = true等)。

对于较大的视频,您可能需要添加一些回调,如oncanplaythrough,等待视频加载足够。