我有兴趣手动(点击按钮)触发使用相同的html视频标记(如果可能)播放不同的YouTube视频网址。
我看到这段代码让我希望这是可行的:http://jsfiddle.net/mister_ben/g7mrs/
html代码:
<video id="myPlayer" src="" class="video-js vjs-default-skin" controls preload="auto" width="320" height="180" data-setup='{ "techOrder": ["youtube"], "src": "http://www.youtube.com/watch?v=D36JUfE1oYk" }'></video>
<p><a href="#" onClick="loadVideo()">Another video</a></p>
js code:
function loadVideo(event) {
videojs("myPlayer").src({ type: "video/youtube", src: "http://www.youtube.com/watch?v=dQw4w9WgXcQ"});
event.preventDefault();
}
但这是旧代码(videojs版本4.1和videojs- youtube插件的旧版本),升级到当前版本只是不起作用。
我正在使用youtube插件(https://github.com/eXon/videojs-youtube)进行videojs。
现在使用新的api是否可能?
答案 0 :(得分:2)
要使用Videojs播放器以离子播放不同的youtube网址。
以下是其工作示例:
page.html
<video id='video' width="100%" controls preload="auto" class="video-js vjs-default-skin vjs-big-play-centered vjs-16-9"></video>
page.ts
this.videoJSplayer = videojs(document.getElementById('video'), {
controls: true,
autoplay: true,
techOrder: ["youtube"],
height: "200"
});
this.videoJSplayer.src({type:'video/youtube',src:"https://www.youtube.com/watch?v=Xwo-H5krlj0"})
答案 1 :(得分:1)
我问作者,他在这里提供了一个例子: https://github.com/eXon/videojs-youtube/issues/339#issuecomment-164592838
这是一个工作的例子:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="http://vjs.zencdn.net/5.4.4/video-js.css"/>
</head>
<body>
<video
id="vid1"
class="video-js vjs-default-skin"
controls
width="640" height="264"
data-setup='{ "techOrder": ["youtube"], "sources": [{ "type": "video/youtube", "src": "https://www.youtube.com/watch?v=xjS6SftYQaQ"}] }'
>
</video>
<button id="change">change video</button>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://vjs.zencdn.net/5.4.4/video.js"></script>
<script
src="https://raw.githubusercontent.com/eXon/videojs-youtube/637a2916c2c4fd2b5fc55dafa3df923a92fec6d0/src/Youtube.js"></script>
<script>
(function ($) {
$(document).ready(function () {
// An example of playing with the Video.js javascript API
// Will start the video and then switch the source 3 seconds latter
// You can look at the doc there: http://docs.videojs.com/docs/guides/api.html
videojs('vid1').ready(function () {
var myPlayer = this;
myPlayer.src({type: 'video/youtube', src: 'https://www.youtube.com/watch?v=y6Sxv-sUYtM'});
$("#change").on('click', function () {
myPlayer.src({type: 'video/youtube', src: 'https://www.youtube.com/watch?v=09R8_2nJtjg'});
});
});
});
})(jQuery);
</script>
</body>
</html>