Sorry for my lack of knowledge on this topic
I have a script that changes the source of the video player. And it does just that. The only problem is that the Video.js player plays the first source assigned to it.
JavaScript
x
5
1
document.getElementById("vid-player_html5_api").innerHTML = "";
2
3
document.getElementById("vid-player_html5_api").innerHTML = "<source src='" + link + "' type='video/mp4'>";
4
document.getElementById("vid-player_html5_api").muted = false;
5
So if there were two buttons, and you clicked Button 1 it would change the source of the player and show the correct video. Then lets say you clicked Button 2 it would change the source of the player, but it would still show the same video it showed for Button 1
It is proven that it changes the source, I checked the Chrome Dev tools and surely enough it changed the source
How do I fix this?
Advertisement
Answer
You can try as below,
JavaScript
1
13
13
1
function playVideo(videoSource, type) {
2
var videoElm = document.getElementById('testVideo');
3
var videoSourceElm = document.getElementById('testVideoSource');
4
if (!videoElm.paused) {
5
videoElm.pause();
6
}
7
8
videoSourceElm.src = videoSource;
9
videoSourceElm.type = type;
10
11
videoElm.load();
12
videoElm.play();
13
}
JavaScript
1
6
1
<video id="testVideo" width="400" controls>
2
<source id="testVideoSource">
3
</video>
4
<br/>
5
<input type="button" value="Play Video 1" onclick="playVideo('http://www.w3schools.com/html/mov_bbb.mp4', 'video/mp4')" />
6
<input type="button" value="Play Video 2" onclick="playVideo('http://www.w3schools.com/html/mov_bbb.ogg', 'video/ogg')" />