Hi All: I have a relatively simple page with an HTML5 video in a JQuery dialog div. I'm trying to use a button to simultaneously open the dialog and start the video playing, but everything I've tried won't play the video until I've clicked the button a second time.
I want the video to start playing as soon as the dialog is opened, without having to click a second "play" button.
<head>
<script type="text/javascript">
$(function () {
$("#vid").dialog({
autoOpen: false, show: {
effect: "blind",
duration: 1000
}, modal: false, height: 480, width: 640, dialogClass: "no-close"
});
$("#lnkVid").click(function () {
$("#vid").dialog("open");
startPlay();
});
function startPlay() {
var vid = document.getElementById('introVid');
if (vid.readyState===4) {
vid.play();
} else {
alert("not ready");
}
}
});
</script>
</head>
<body>
<a href="#" id="lnkVid">Video</a>
<div id="vid" >
<video width="640" height="480" id="introVid" controls>
<source src="myvid.mp4" type="video/mp4"/>
Your browser does not support the video tag.
</video>
</div>
</body>
WHen I click on the "linkVid" link, the modal opens and the video is at the starting point, but doesn't play. It's almost as if the play() command is pausing it.
If I click on the same link a second time, the video will play.
I've tried using combinations of play() and settimeout to delay the play command, and also tried using the ternary
vid.paused() ? vid.play() : vid.pause();
but that doesn't work either...I presume because the video isn't "paused" when it first loads?
I'm not sure if the dialog itself is interfering with the "play" command?