I have found this helpful piece of code from the MartinJHiggings website (thank you).
The code is designed to play streaming audio. But it was designed when autoplay in browsers was allowed. It is no longer. But the default image is still 'pause' as it thinks audio is playing.
I basically want the default image to be 'play', which when clicked it should change to 'pause' and start the stream.
When clicked again, the image should change to 'play' and the stream should stop.
Any idea how I can do this please?
<script type="text/javascript">
function audioControl() {
var playdiv = document.getElementById('playdiv');
var pausediv = document.getElementById('pausediv');
var myAudio = document.getElementById('myAudio');
if (myAudio.paused) {
myAudio.play();
pausediv.style.display = 'block';
playdiv.style.display = 'none';
} else {
myAudio.pause();
pausediv.style.display = 'none';
playdiv.style.display = 'block';
}
}
</script>
<audio id="myAudio" autoplay preload="metadata">
<source src="http://IP:PORT/stream"></source>
Unfortunately your browser doesn't support html5 audio streaming, please update your browser.
</audio>
<button id="control" class="control" onclick="audioControl()">
<div id="playdiv" style="display:none">
<img src="play.png" width="96" height="96" alt="Play"/>
</div>
<div id="pausediv" style="display:block">
<img src="pause.png" width="96" height="96" alt="Pause"/>
</div>
</button>
You should remove the autoplay attribute on your audio element and then you should switch your divs style:
playdiv should be with style="display:block"
pausediv should be with style="display:none"
function audioControl() {
var playdiv = document.getElementById('playdiv');
var pausediv = document.getElementById('pausediv');
var myAudio = document.getElementById('myAudio');
if (myAudio.paused) {
myAudio.play();
pausediv.style.display = 'block';
playdiv.style.display = 'none';
} else {
myAudio.pause();
pausediv.style.display = 'none';
playdiv.style.display = 'block';
}
}
<audio id="myAudio" preload="metadata">
<source src="https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3"/>
Unfortunately your browser doesn't support html5 audio streaming, please update your browser.
</audio>
<button id="control" class="control" onclick="audioControl()">
<div id="playdiv" style="display:block">
<img src="play.png" width="96" height="96" alt="Play"/>
</div>
<div id="pausediv" style="display:none">
<img src="pause.png" width="96" height="96" alt="Pause"/>
</div>
</button>
Also available on JSFiddle.
Related
When I click on the play/pause button nothing seems to happen, after clicking on the video I want to play, the vs code is not finding me any error nor the dev console.
I don't know what the issue is with the play/pause button.
Also, there is some issue with the third video, when I click on the third video it gives the following error:
Failed to load resource: the server responded :5500/favicon.ico:1 with a status of 404 (Not Found)
window.onload = init;
function init() {
var frame1 = document.querySelector('#video1');
frame1.innerHTML += ' <br> <button id="play">play </button> <button id="pause">pause</button> <button id="volume-up">volume up</button> <button id="volume down">volume-down</button> <button id="mute">mute</button> ';
}
//to play another video
var click = document.querySelectorAll('#videos > div');
click.forEach(function(elem) {
elem.addEventListener('click', nextPlaylist);
function nextPlaylist(evt) {
var save = elem.querySelector('.video').currentSrc;
var save1 = elem.querySelector('.video');
elem.innerHTML = `<video class="video" > <source src="${save}"> </video> <br> <button id="play">play </button> <button id="pause">pause</button> <button id="volume-up">volume up</button> <button id="volume down">volume-down</button> <button id="mute">mute</button> `;
var play = document.querySelector('#play');
play.addEventListener('click', playVid);
var pause = document.querySelector('#pause');
pause.addEventListener('click', pauseVid);
function playVid() {
save1.play();
}
function pauseVid() {
save1.pause();
}
}
}
);
<body>
<header class="head">
VIDEO PLAYER
</header>
<div id="videos">
<div id="video1">
<video class="video" controls>
<source src="https://www.videvo.net/videvo_files/converted/2018_04/preview/180301_06_A_CityRoam_03.mp420186.webm">
</video>
</div>
<div id="video2">
<video class="video" controls>
<source src= "https://www.videvo.net/videvo_files/converted/2016_01/preview/Forest_15_2_Videvo.mov92730.webm">
</video>
</div>
<div id="video3">
<video class="video" controls>
<source src="https://www.videvo.net/videvo_files/converted/2016_09/preview/160820_125_NYC_OutOfFocusCarLights5_1080p.mp444096.webm">
</video>
</div>
</div>
<script src="video.js"></script>
</body>
There are several issues with the code.
You declare the video element but then override it.
var save1= elem.querySelector('.video');
but then, you remove that element and render a different one
elem.innerHTML = `<video class="video" >...
So you need to retrieve it again (or just move var save1= elem.querySelector('.video'); after the line above.
When the user clicks on the buttons the parent's listener (elem.addEventListener('click', nextPlaylist); also get fired, so it re-render the videos.
To avoid that, use stopPropagation to prevent from the event from propagate to the parent
You have multiple id attribute with the same value (when the user clicks on more than one video) so document.querySelector catch the first one (which wasn't work too because the reasons above).
Replace the ids to classes and use elem.querySelector instead of document.querySelector to make sure it will find the buttons in a specific div.
//to play another video
var click = document.querySelectorAll('#videos > div');
click.forEach(function(elem) {
elem.addEventListener('click', nextPlaylist);
});
function nextPlaylist(evt) {
const elem = evt.currentTarget;
var save = elem.querySelector('.video').currentSrc;
elem.innerHTML = `<video class="video" > <source src="${save}"> </video> <br> <button class="play">play </button> <button class="pause">pause</button> <button id="volume-up">volume up</button> <button id="volume down">volume-down</button> <button id="mute">mute</button> `;
var save1 = elem.querySelector('.video');
var play = elem.querySelector('.play');
play.addEventListener('click', playVid);
var pause = elem.querySelector('.pause');
pause.addEventListener('click', pauseVid);
function playVid(e) {
e.stopPropagation();
save1.play();
}
function pauseVid(e) {
e.stopPropagation();
save1.pause();
}
}
<header class="head">
VIDEO PLAYER
</header>
<div id="videos">
<div id="video1">
<video class="video" controls>
<source src="https://www.videvo.net/videvo_files/converted/2018_04/preview/180301_06_A_CityRoam_03.mp420186.webm">
</video>
</div>
<div id="video2">
<video class="video" controls>
<source src= "https://www.videvo.net/videvo_files/converted/2016_01/preview/Forest_15_2_Videvo.mov92730.webm">
</video>
</div>
<div id="video3">
<video class="video" controls>
<source src="https://www.videvo.net/videvo_files/converted/2016_09/preview/160820_125_NYC_OutOfFocusCarLights5_1080p.mp444096.webm">
</video>
</div>
</div>
By the way, you can move the function out. It's a shame to create new function for each element instead of only one.
https://jsbin.com/koritub/edit?html,js,output
i want to play video when item gets active class in bootstrap slider below is my code
<div class="item" id='mobileapp'>
<video loop controls width="100%" id='video'>
<source src="../../vid.mp4" width="100%" height="100%" type="video/mp4"> Your browser does not support the video tag.
</video>
<div class="overlay"></div>
<div class="carousel-caption">
<h1 class="super-heading text-white">Heading</h1>
<div class='row'>
<div class='col-sm-2 col-sm-offset-1'>
<img src='../img/googleplay.png' class='img-responsive hidden-xs' />
</div>
<div class='col-sm-2'>
<img src='../img/applestore.png' class='img-responsive hidden-xs' />
</div>
</div>
</div>
</div>
Js
<script>
$(document).ready(function () {
if ($('#mobileapp').hasClass('active')) {
$('#video')[0].play();
}
else {
$('#video')[0].pause();
}
})
</script>
active class gets added but it does not pay video
the functions 'play' and 'pause' are not jquery functions, they belong to the DOM element. Now to make it work try to execute your code every 10 miliseconds or something like that.
Try:
var videoElt = document.getElementById('video');
$(document).ready(function () {
setInterval(function () {
if ($('#mobileapp').hasClass('active')) {
videoElt.play(); // to play the video
}
else {
videoElt.pause(); // to pause the video
}
}, 10);
});
My page contains two HTML5 video players - each with their own controller button below using JS.
Both sets of buttons only work with one of the players (the second one) - I know I need to name each player and buttons but I have tried many combos and can't get it to work. Code below…
<div style="text-align:center">
<video poster="poster1.png" id="video1" width="90%">
<source src="video1.mp4" type="video/mp4">
</video><br>
<button onclick="playPause()">Play/Pause</button>
<br><br>
</div>
<script>
var myVideo = document.getElementById("video1");
function myVideo.playPause() {
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
</script>
<div style="text-align:center">
<video poster="poster2.png" id="video2" width="90%">
<source src="video2.mp4" type="video/mp4">
</video><br>
<button onclick="playPause()">Play/Pause</button>
<br><br>
</div>
<script>
var myVideo = document.getElementById("video2");
function playPause() {
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
</script>
Easiest way would be to pass the id you want into the script (so you only need the script to exist once):
<script>
function playPause(vidId) {
var myVideo = document.getElementById(vidId);
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
</script>
<div style="text-align:center">
<video poster="poster1.png" id="video1" width="90%">
<source src="trailer.mp4" type="video/mp4">
</video><br>
<button onclick="playPause('video1')">Play/Pause</button>
<br><br>
</div>
<div style="text-align:center">
<video poster="poster2.png" id="video2" width="90%">
<source src="trailer.mp4" type="video/mp4">
</video><br>
<button onclick="playPause('video2')">Play/Pause</button>
<br><br>
</div>
I can currently play and pause a video by clicking anywhere in the <video> tag.But unfortunately, when i have added a second video the function works only for the first video only.
var video = document.getElementById('vid');
video.addEventListener('click', function() {
this.paused?this.play():this.pause();
},
false);
video.addEventListener("pause", function() {
document.getElementById("paused").style.display = "";
});
video.addEventListener("play", function() {
document.getElementById("paused").style.display = "none";
});
<div id="main">
<div id="computer" class="content">
<div style="margin-left:8%">
<div id="paused" style="display: none">QUESTION HERE</div>
<video width="1000" height="480" id="vid">
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
</div>
<div id="ip" class="content">
<div style="margin-left:8%">
<div id="paused" style="display: none">QUESTION HERE</div>
<video width="1000" height="480" id="vid">
<source src="video2.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
</div>
</div>
Any issue with the js code. Kindly advice.
it will not work lake that because ,the interpreter will consider just the last id with the same element ,if you use just two videos then you can just add other video id and paused id
<div id="ip" class="content">
<div style="margin-left:8%">
<div id="paused2" style="display: none">QUESTION HERE</div>
<video width="1000" height="480" id="vid2">
<source src="video2.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
try to use classes instud of ids
for more detaill take a lok to this page
http://flash.flowplayer.org/demos/installation/multiple-players.html
In my video template site, I want to change the source tag of the video when I'm clicking on HQ, only the video should start playing, without a click on the play button, and the text of the link should change to nq in the same function.
Following the html code
{{if hqnq == true }} //jquery template variable no importance
<div class="toolbar" id="controls">
<div style="float:right">
HQ
// the text of the link should change when you are clicking
</div>
<div style="clear:both"></div>
</div>
{{/if}}
<div id="video_player">
<video autobuffer controls autoplay>
<source src="http://www.tools4movies.com/Droid/Default/Inception.mp4" type="video/mp4" width="auto" height="auto" id="video" />
</video>
</div>
And at least there should be a function that will be performing the task. But how to do that?
<script type="text/javascript">
var button = document.getElementById('player_hq_nq');
function showVideo() {
if (button.title == "nq") {
document.getElementById("video").setAttribute("src", "http://www.tools4movies.com/Droid/Default/Inception.mp4");
document.getElementById("video").load();
document.getElementById("video").play();
} else {
document.getElementById("video").setAttribute("src", "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
document.getElementById("video").load();
document.getElementById("video").play();
button.title = "nq";
}
}
</script>
Here is the fiddle: http://jsfiddle.net/k68Zp/389/
You need to call showVideo() onclick of HQ. so you need to add following code just above the showVideo() function.:
$(document).ready(function(){
$("#player_hq_nq").click(function(){
showVideo();
});
});