Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Load and Play HTML5 video

I'm trying to load and play a HTML5 video onClick event. But i'm not able to accomplish this :-/

Here's my HTML5:

<div id="divVideo">
    <video id="video" controls width="560">
        <source id="mp4" type="video/mp4" />
    </video>
</div>

<div onclick="loadVideo('Muse-Animals.mp4');">play</div>

Here's my JS:

function loadVideo(id)
{
    var video = document.getElementById('video');
    var mp4 = document.getElementById('mp4');

    mp4.src = "vidz/" + id;

    video.load();
    video.play();
}

I checked the element and it does update the video tag properties, but doesn't load or play the video.

What am i doing wrong?

like image 824
Ricky Avatar asked Sep 09 '13 17:09

Ricky


People also ask

How do I play HTML5 videos?

Before HTML5 videos were only played using the Flash Player. You can view HTML5 videos on all popular browsers such as Google Chrome, Internet Explorer, Mozilla Firefox, and Safari.

Can we play video in HTML5?

With the introduction of HTML5, you can now place videos directly into the page itself. This makes it possible to have videos play on pages that are designed for mobile devices, as plugins like Adobe Flash Player don't work on Android or iOS.

How do I make a video playable in HTML?

HTML allows playing video in the web browser by using <video> tag. To embed the video in the webpage, we use src element for mentioning the file address and width and height attributes are used to define its size. Example: In this example, we are using <video> tag to add video into the web page.


1 Answers

First of all check if you are giving the video src static then it is getting played or not. if it is getting played then while giving it dynamically try giving an extra variable in the src to make the video source refresh itself

like

function loadVideo(id)
{
var video = document.getElementById('video');
var mp4 = document.getElementById('mp4');
d = new Date();

mp4.src = "vidz/" + id + d.getTime();

video.load();
video.play();
}
like image 132
Vinay Pratap Singh Bhadauria Avatar answered Oct 14 '22 23:10

Vinay Pratap Singh Bhadauria