Switch video source - same time on slider - JS, HTML5 Video - javascript

I have a JS function that changes the video source of an HTML video. A button activates this function. It loads the same new video on switch. There are 2 videos of the same length.
How do I:
interchange the video every time I click the button?
when i click the button, can the video load at the same time the previous video was playing?
HTML:
<button onclick="myFunction()" type="button">Change Video</button><br>
<video id="myVideo" controls autoplay>
<source id="mp4_src" src="video1.mp4" type="video/mp4">
<source id="mp4_src" src="video2.mp4">
</video>
JS
var vid = document.getElementById("myVideo");
function myFunction() {
vid.src = "video2.mp4";
vid.load();
}

Here is the fiddle that solves both of your problems, http://jsfiddle.net/egjyd9rs/5/
Basically, the toggle function which takes care of both is as below,
function myFunction() {
currentlPlayingTime = vid.currentTime;
if (currentlyPlaying === 1) {
vid.src = src2;
currentlyPlaying = 2;
statusElement.innerText = 'Going to play video2..';
} else {
vid.src = src1;
currentlyPlaying = 1;
statusElement.innerText = 'Going to play video1..';
}
vid.load();
vid.addEventListener('loadedmetadata', function () {
vid.currentTime = currentlPlayingTime;
}, false);
}

Related

How to use vid.onended to detect when a video is done playing using javascript

I am trying to create an HTML video playlist and currently I am using vid.onended to detect when a video is done playing (based of the current video src) and then play the next video when the video ends. This works perfectly for the first video but for some reason it never plays the second video and jumps straight to the third video.
My code:
//add video playlist functionality to auto play next video based on id
var vid = document.getElementById("urlVideo");
vid.onended = function() {
var video0 = "http://techslides.com/demos/sample-videos/small.mp4";
var video1 = "https://media.w3.org/2010/05/sintel/trailer.mp4";
var video2 = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
if (vid.src = video0) {
vid.src = video1;
}
if (vid.src = video1) {
vid.src = video2;
}
};
<video id="urlVideo" width="100%" height="460" controls autoplay>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
What am I doing wrong?
Edit:
Answer by Alen Toma works perfectly.
I Also managed to do it according to the current video source based on a comment by Quentin, For anyone else looking for how to do it explicitly with the current video source as the variable/condition, please see
https://jsfiddle.net/redlaw/qjb5h7e9/9/
I did make a small example below, it should help.
Have a look at this JSFiddle.
//add video playlist functionality to auto play next video based on id
var videoSrc = [
"https://media.w3.org/2010/05/sintel/trailer.mp4",
"http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerBlazes.mp4"
]
var vid = document.getElementById("urlVideo");
var index = 0;
vid.addEventListener("ended", function() {
var currentSrc = videoSrc[index];
index += 1;
if (index >= videoSrc.length)
index = 0; // Make Loop and jump to the first video
vid.src = currentSrc;
vid.play();
console.log(currentSrc)
}, true);
<video id="urlVideo" controls autoplay>
<source src="http://techslides.com/demos/sample-videos/small.mp4" type="video/mp4">
</video>
you must use an event listener for your video player like this code:
var vid = document.getElementById("urlVideo");
vid.addEventListener("ended", function() { /* your code*/ }, true);

Play a video based on a value from local storage

I am aware that this question might have been asked here a few times, however I could not find a solution I've been looking for. We have a requirement where the users should be able to pause the video and resume from where they left.
I was able to get to the point where I am able to store and fetch the paused time from local storage, however I have been facing challenges playing the video from the stored value. Below is the code.
<video id="myVideo" width="300" height="300" controls>
<source src="Bunny.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video><br />
<button onclick="PlayVideo()" type="button" id="PlayVideo">Play</button>
<script>
var vid = document.getElementById("myVideo")
function getCurTime() {
return vid.currentTime
}
var isPlaying = false
var PausedTime
function PlayVideo() {
var change = document.getElementById("PlayVideo")
if (isPlaying) {
vid.pause()
//Storing the paused time to local storage
localStorage.setItem('CaptureTime', getCurTime())
//Get the saved time from local storage
PausedTime = localStorage.getItem('CaptureTime')
change.innerHTML = "Play"
}
else {
vid.play()
change.innerHTML = "Pause"
}
isPlaying = !isPlaying
}
</script>
Would appreciate if anyone here could help me out with this.
Please let me know if more details are needed.
You should only get the time when playing, and save on pause. Set the current time to the local storage paused time when being played. Like this:
var vid = document.getElementById("myVideo");
function getCurTime() {
return vid.currentTime;
}
var isPlaying = false;
var PausedTime;
function PlayVideo() {
var change = document.getElementById("PlayVideo");
//Get the saved time from local storage
var PausedTime = localStorage.getItem('CaptureTime');
if (isPlaying) {
vid.pause();
//Storing the paused time to local storage
localStorage.setItem('CaptureTime', getCurTime());
change.innerHTML = "Play";
}
else {
vid.currentTime = PausedTime;
vid.play();
change.innerHTML = "Pause";
}
isPlaying = !isPlaying;
}
<video id="myVideo" width="300" height="300" controls>
<source src="Bunny.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video><br/>
<button onclick="PlayVideo()" type="button" id="PlayVideo">Play</button>
To pause and play a video in javascript you don't actually need to save the time to local storage.
That being said, if you still want to do it that way, you would just need to remember these lines:
localStorage.setItem('CaptureTime', getCurTime());
PausedTime = localStorage.getItem('CaptureTime');
vid.currentTime = PausedTime;
for reference
In addition, when I tried your code it wouldn't change the play to pause, so I made a few adjustments.
This is how I implemented it all:
<html>
<body>
<button id="controls" onclick="play()" type="button">Play Video</button><br>
<video id="myVideo" width="320" height="176">
<source src="Bunny.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<script>
var vid = document.getElementById("myVideo");
var isPlaying = false; //or this could be: var isPaused = vid.paused;
function play() {
if(!isPlaying){
vid.play();
document.getElementById("controls").innerHTML = "Pause Video";
}
else{
vid.pause();
document.getElementById("controls").innerHTML = "Play Video";
}
isPlaying = !isPlaying;
}
</script>
</body>
</html>
Check out these pages for more:
https://www.w3schools.com/js/js_htmldom_html.asp
https://www.w3schools.com/tags/av_met_play.asp
https://www.w3schools.com/html/html5_video.asp

Redirect to a page in ended function

I have a javascript code that play a video after another video previously load in the video src. What i want is when the second video ended automatically redirect me to other page, here is my code:
<script type="text/javascript">
var videoplayer = document.getElementById("videoplayer");
var video = $('video')[0];
videoplayer.addEventListener('click',function(){
videoplayer.play();
},false);
videoplayer.addEventListener('ended',function(){
var nextVideo = "C:/Users/Video Turismo/Desktop/Spots y videos/Vidios/Lamb.mp4";
videoplayer.src = nextVideo;
videoplayer.pause();
$('video').unbind('ended');
window.location("www.google.com");
},false);
</script>
<video width="100%" height="50%" controls id="videoplayer" >
<source src="C:/Users/Video Turismo/Desktop/Spots y videos/Vidios/NEGAS.mp4">
</video>
I think you should ask if the video.src is not already the nextVideo and then change location or whatever. This way you know the second video ended.
var videoplayer = document.getElementById("videoplayer");
var video = $('video')[0];
videoplayer.addEventListener('click', function(){
videoplayer.play();
},false);
videoplayer.addEventListener('ended', function(){
var nextVideo = "C:/Users/Video Turismo/Desktop/Spots y videos/Vidios/Lamb.mp4";
if (videoplayer.src == nextVideo) {
window.location("www.google.com");
}
videoplayer.src = nextVideo;
videoplayer.pause();
}, false);
<video width="100%" height="50%" controls id="videoplayer" >
<source src="C:/Users/Video Turismo/Desktop/Spots y videos/Vidios/NEGAS.mp4">
</video>

sound effect in a html5 game

I'm trying to play sound effect in a game with each hit,but the sound sometimes play and others NOT !!
I'm using the next code :
<script>
var hitSound = new Audio();
function playEffectSound()
{
hitSound = document.getElementById('effects');
hitSound.loop = false;
hitSound.currentTime = 0;
hitSound.play();
}
</script>
<audio id="effects" hidden>
<source src="sound/mp3/effect.mp3" type="audio/mpeg">
<source src="sound/wav/effect.wav" type="audio/wav">
</audio>
any ideas ?
Whenever you are using audio tag try writing its script after audio tag ,If you write script before that audio tag it gives sometime problem in playing the audio,
i would recommend Write Script at the end of the page that's the standard way to write script.
http://jsfiddle.net/LyDWH/4/
<!DOCTYPE html>
<html>
<body>
<audio id="effects" hidden >
<source src="http://www.w3schools.com/html/horse.mp3" type="audio/mpeg">
</audio>
<div onclick= "playEffectSound();">Horse Click Me..!</div>
</body>
<script>
var hitSound = new Audio();
function playEffectSound()
{
hitSound = document.getElementById('effects');
hitSound.currentTime = 0;
hitSound.play();
}
</script>
</html>
​
You could use the following code to play audio
function playEffectSound(sound) {
//Does the sound already exist?
if (document.getElementById(sound) != null) {
document.getElementById(sound).play();
return;
}
//Create elements
var audioElement = document.createElement("audio");
var sourceElement = document.createElement("source");
sourceElement.src = sound + ".mp3";
sourceElement.type = "audio/mpeg";
var sourceElementWave = document.createElement("source");
sourceElementWave.src = sound + ".wav";
sourceElementWave.type = "audio/wav";
//Add sources to audio
document.body.appendChild(sourceElementWave);
document.body.appendChild(audioElement);
audioElement.setAttribute("id", sound);
audioElement.appendChild(sourceElement);
audioElement.loop = false;
audioElement.currentTime = 0;
audioElement.play();
}
You can use it like this: playEffectSound("sounds/effect"); and it will either play sounds/effect.mp3 or sounds/effect.wav
try with each hit to add a new var sound and sound.play() and it will just work fine

Audio use js to use image for playpause btn

I am new to js so forgive me. I'm using the html5 audio tag with some js to only show play and pause as a button. I would like to change the text for play & pause out with images. Like playbtn.png and pausebtn.png. Here is where I'm stuck.
html:::
<!-- hidden audio player -->
<audio id="audio" controls autoplay hidden="">
<source src="music/eatForTwo.wav" type="audio/wav">
<source src="music/eatForTwo.mp3" type="audio/mpeg">
<p>Your Browser Doesn't Support The HTML5 audio Element</p>
</audio>
<button id="playpause" title="play" onclick="togglePlayPause()" ><img id="imgbtn" src="music/pausebtn.png"/></button>
<!-- javascript for audio control
-->
<script src="code/html5audio.js" type="text/javascript">
</script>
js:::
// Grab a handle to the video
var audio = document.getElementById("audio");
// Turn off the default controls
audio.controls = false;
function togglePlayPause() {
var playpause = document.getElementById("playpause");
if (audio.paused || audio.ended) {
playpause.title = "pause";
playpause.style.backgroundImage = "url(music\pausebtn.png)";
audio.play();
}
else {
playpause.title = "play";
playpause.style.backgroundImage = "url(music\playbtn.png)";
audio.pause();
}
}
Edit your js as following but you have to change url(c:\folder\img.jpg) to url(yourPath) .... Now yourPath does not mean yourPath it means the location of the image on your hard disk
// Grab a handle to the video
var audio = document.getElementById("audio");
// Turn off the default controls
audio.controls = false;
function togglePlayPause() {
var playpause = document.getElementById("playpause");
if (audio.paused || audio.ended) {
//playpause.title = "pause";
playpause.style.backgroundImage = "url(c:\folder\img.jpg)";
audio.play();
}
else {
//playpause.title = "play";
playpause.style.backgroundImage = "url(c:\img1.jpg)";
audio.pause();
}

Categories