How to get .load() working within .each() loop? - javascript

I'm modifying a bit of code I found here from this question.
I am trying to get it working now for multiple instances of a video. This is the only way I can get it working but I would like to make it work for each instance that there is a video which changes on each page this applies on.
$( function() {
const bgv = $('.bgvid');
$('source', bgv).each(function() {
const el = $(this);
el.attr('src', el.data('src'));
});
bgv[0].load();
bgv[1].load();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
</video>
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
</video>
I have tried putting it within a loop like this instead of the bgv[0].load(); but it didn't work:
$('.bgvid').each(function(){
$(this).load();
});

Just call this.load(), don't convert the DOM element into a jQuery element.
$( function() {
const bgv = $('.bgvid');
$('source', bgv).each(function() {
const el = $(this);
el.attr('src', el.data('src'));
});
bgv.each(function() {
this.load();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
</video>
<video class="bgvid" playsinline="playsinline" loop="loop" autoplay="autoplay" muted="muted" preload="none">
<source type="video/mp4" data-src="https://archive.org/download/UNIVAC-AD-2/UNIVAC2_512kb.mp4">
</video>

Related

Random video play code on homepage suddenly stopped working

I'm in charge of maintaining the website of a small foundation dedicated to video art.
On the homepage we used to have a few videos playing randomly when the user clicked on play.
But suddenly a while back it stopped working , the videos won't load on Chrome /Edge /Vivaldi while on Firefox it plays only the last video in the list ...
<div class="videosaccueil">
<section id="videos">
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video1.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video2.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video3.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video4.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video5.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video6.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video7.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video8.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video9.mp4"></video>
<video playsinline="" controls="controls" preload="none"><source type="video/mp4" src="/wp-content/uploads/2018/videos/video10.mp4"></video>
</section> <script><br />
(function () {<br />
"use strict";<br />
var videosSection = document.getElementById("videos");<br />
var videosSectionClone = videosSection.cloneNode();<br />
var videos = videosSection.getElementsByTagName("video");<br />
var randomVideo = videos.item(Math.floor(Math.random() * videos.length)).cloneNode(true);<br />
randomVideo.setAttribute("preload", "auto");<br />
videosSectionClone.appendChild(randomVideo);<br />
videosSection.parentNode.replaceChild(videosSectionClone, videosSection);<br />
randomVideo.play();<br />
})();<br />
</script>
</div>
I wasn't the one that made the random play code and I'm not practical enough to understand what went wrong so It would be really nice if someone can indicate to me how to fix the code if it's possible .
Thank you very much for your help .

how to make an array with several id's to a video tag with javascript jquery

I am using a custom build html5 video. The video tag has an unique id. Now i want to create several video tags.
This is how it works wit only 1 video:
<video id="myVideo1" controls preload="auto" width="600" height="350" >
<source src="test.mp4" type="video/mp4" />
</video>
the js:
$(document).ready(function(){
var video = $('#myVideo1);
// stuff goes her...
So: how can i make several video tags with each his own id and trigger with javascript on these id's?
How to handle with several videos, independent from each other like below?
<video id="myVideo1" controls preload="auto" width="600" height="350" >
<source src="test1.mp4" type="video/mp4" />
</video>
<video id="myVideo2" controls preload="auto" width="600" height="350" >
<source src="test2.mp4" type="video/mp4" />
</video>
<video id="myVideo3" controls preload="auto" width="600" height="350" >
<source src="test3.mp4" type="video/mp4" />
</video>
Code to start the video:
//bind video events
$('.videoContainer')
.append('<div id="init"></div>')
.hover(function() {
$('.control').stop().animate({'bottom':0}, 500);
$('.caption').stop().animate({'top':0}, 500);
}, function() {
if(!volumeDrag && !timeDrag){
$('.control').stop().animate({'bottom':-45}, 500);
$('.caption').stop().animate({'top':-45}, 500);
}
})
.on('click', function() {
$('#init').remove();
$('.btnPlay').addClass('paused');
$(this).unbind('click');
video[0].play();
});
$('#init').fadeIn(200);
});
You can always select the actual element instead of relying on id's.
$(document).ready(function(){
$('video').each(function(){
var video = $(this);
// Do the rest of the instructions.
});
});

Firefox bug - video does not mute when it is cloned with jQuery?

Firefox does not mute the video when the video element is cloned with jQuery.
JS:
$( document ).ready(function() {
var origin = $('.item-video');
var target = $('.clone');
origin.clone(true).appendTo(target);
origin.empty();fix
});
HTML:
<div class="item-video">
<video width="560" height="315" autoplay muted controls loop>
<source src="big-buck-bunny_trailer.webm" type="video/webm">
<!-- <source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm"> -->
</video>
</div>
<div class="clone">
</div><div class="item-video">
<video width="560" height="315" autoplay muted controls loop>
<source src="big-buck-bunny_trailer.webm" type="video/webm">
<!-- <source src="http://video.webmfiles.org/big-buck-bunny_trailer.webm" type="video/webm"> -->
</video>
</div>
<div class="clone">
</div>
jsfiddle
Any ideas how I can fix the bug on Firefox?
I don't know why this is happening on FF but this is the workaround
origin.clone(true).appendTo(target).find('video').attr('onloadedmetadata','this.muted =true');

HTML5 Video autoplay in Android

I am developing an html5 player supports ads with video-js
Since i know it doesn't work in iOS, is it the same on Android?
So far i've tried these with no luck on autoplay in Android
HTML
<video id="inReadPlayer" autoplay loop="loop"
controls preload="auto" class="video-js vjs-default-skin" width="640"
height="360" > <source src="'+videoSource+'" type="video/mp4">
</video>
JS
var player = videojs('inReadPlayer', { /* Options */ }, function() {
this.play();
this.on('ended', function() {
});
});
Thanks in advance
Try this:
var videoplayer = document.getElementById('inReadPlayer')
videoplayer.addEventListener("load", function() {
videoplayer.play();
});
videoplayer.onended = function() {
source.setAttribute('src', 'MAINVIDEO.mp4');
};
<video id="inReadPlayer" autoplay loop="loop"
controls preload="auto" class="video-js vjs-default-skin" width="640"
height="360" > <source src="'+videoSource+'" type="video/mp4">
</video>
Note that this snippet won't work because there is no video source

Javascript HTML5 audio - Dont play sound if already playing?

I have set up some HTML audio on my site and I have set an autoplay function after a couple of seconds, however the sound seems to be loading twice? one starts playing straight after the other although, one is a ghost and cannot see a player for it.
How could I go about telling javascript not to start playing sounds if there are already sounds playing?
<audio controls="controls" onloadeddata="var audioPlayer = this; setTimeout(function() { audioPlayer.play(); }, 2000)" }>
<source src="http://www.alanis50.com/Music/music.mp3" type="audio/mp3"/>
<source src="http://www.alanis50.com/Music/music.ogg" type="audio/ogg"/>
</audio>
The above issue has been solved however I wanted to add a flash fallback for this audio for internet explorer and so added:
<div id ="audio">
<script>var readingMusic = false; </script>
<audio controls="controls" loop="loop" onloadeddata="var audioPlayer = this; if (!readingMusic){readingMusic = true;setTimeout(function() { audioPlayer.play(); }, 2000)}" }>
<source src="http://www.alanis50.com/Music/music.mp3" type="audio/mp3"/>
<source src="http://www.alanis50.com/Music/music.ogg" type="audio/ogg"/>
<embed type="application/x-shockwave-flash" flashvars="audioUrl=http://www.alanis50.com/Music/music.mp3&autoPlay=true" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="400" height="27" quality="best" wmode="transparent"></embed>
</audio>
</div>
<div id="audiohide">
<audio controls="controls" loop="loop" onloadeddata="var audioPlayer = this; if (!readingMusic){readingMusic = true;setTimeout(function() { audioPlayer.play(); }, 2000)}" }>
<source src="http://www.alanis50.com/Music/music.mp3" type="audio/mp3"/>
<source src="http://www.alanis50.com/Music/music.ogg" type="audio/ogg"/>
<embed type="application/x-shockwave-flash" flashvars="audioUrl=http://www.alanis50.com/Music/music.mp3&autoPlay=true" src="http://www.google.com/reader/ui/3523697345-audio-player.swf" width="400" height="27" quality="best" wmode="transparent"></embed>
</audio>
</div>
But now the flash audio is behaving how the previous audio did obviously the javascript is not affecting the flash embed?
Can't you have a boolean telling the browser if there is already some music playing ?
Example with 2 tags:
<script>var readingMusic = false; </script>
<audio controls="controls" onloadeddata="var audioPlayer = this; if (!readingMusic){readingMusic = true;setTimeout(function() { audioPlayer.play(); }, 2000)}" }>
<source src="http://www.alanis50.com/Music/music.mp3" type="audio/mp3"/>
<source src="http://www.alanis50.com/Music/music.ogg" type="audio/ogg"/>
</audio>
<audio controls="controls" onloadeddata="var audioPlayer = this; if (!readingMusic){readingMusic = true;setTimeout(function() { audioPlayer.play(); }, 2000)}" }>
<source src="http://www.alanis50.com/Music/music.mp3" type="audio/mp3"/>
<source src="http://www.alanis50.com/Music/music.ogg" type="audio/ogg"/>
</audio>
Well, that's a part of your jScript.js.
It seems that problem is in this line:
location = base + "/#" + current.replace(base, "");
You navigating to the new url without leaving current page(if origins are the same and the only difference is in hashes). I recommend you to start playing audio after changing location origin.
So you might place your autoplay logick right after this location changes.

Categories

Resources