Background video doesn't work on hover in Chrome - javascript

I'm trying to accomplish background video playing with sound when I hover a link. Actually it works fine in all browsers except Chrome. What's going on in Chrome is I see the first frame of my video when
I hover a link and nothing more. Reloading the page doesn't solve the problem. But clicking on logo to refresh the page do solve the problem. When I add "muted" to video tag it also works fine. I read that Chrome blocks video and audio autoplay by default (I have 66.0.3359.181 Version of Chrome).
So is there any solution to play background video with sound in Chrome on first entrance?
You can check out the website example here:
http://s90685xj.beget.tech/
The code I'm using:
var viewport = document.getElementsByClassName('fade')[0]
var video = document.getElementById('video')
viewport.addEventListener('mouseover', function() {
video.play()
}, false);
viewport.addEventListener('mouseout', function() {
video.pause()
}, false);
document.getElementById('video').addEventListener('loadedmetadata', function() {
this.currentTime = 24;
}, false);
New York<span><video id="video" class="video-background"><source src="http://s90685xj.beget.tech/wp-content/uploads/2018/05/Hernandos-Hideaway-Broadway-Dreams-NYC-Showcase-2016.mp4"></source></video></span>
Thanks in advance for any help!

Related

Why can't I get this video to automatically start on page load on mobile?

I have this video as a background with Bootstrap that I would like to autoplay on page load. It works fine with desktop Chrome, but not so much for mobile.
It is a small, .mp4 file that is created using:
<video class="video-fluid" id="video1" autoplay loop>
<source src="videos/light-swaying.mp4" type="video/mp4" />
</video>
The only thing that shows up on mobile is just a still of the first frame, not a moving video. I added this script at the bottom of my body, and it actually starts the video fine. It loops and everything:
<script type="text/javascript">
var video = document.getElementById('video1');
var aboveView = document.getElementById('video-carousel-example2');
aboveView.addEventListener('click', function () {
video.autoplay = true;
video.play();
}, false);
</script>
The only problem is, though, I have to click on the video to actually start it. I tried this script in the same spot, and no luck whatsoever:
<script type="text/javascript">
var video1 = document.getElementById('video1');
video1.autoplay = true;
video1.play();
</script>
So does anyone have any idea how to fix this and make it play automatically?
Which version of Chrome? Because:
"Autoplay was disabled in previous versions of Chrome on Android because it can be disruptive, data-hungry and many users don't like it."
Source:
https://developers.google.com/web/updates/2016/07/autoplay
Mostly we too find this issues in ipad and tablets because ipad and tablet doesnt support the autoplay
To see a full list of these restrictions, see the official docs: https://webkit.org/blog/6784/new-video-policies-for-ios/
for that you need to trigger your click function by checking your monitor resolution i am not familiar with javascript so i am written this using jquery
$(document).ready(function(){
var screenWidth = $( window ).width();
if(screenWidth <= 1024)//just i am checking for ipad only
{
$('#video-carousel-example2).trigger('click');
}
$('#video-carousel-example2).click(function(){
var video1 = document.getElementById('video1');
video1.play();
})
});

HTML5 video won't restart on Mobile Safari

I'm having an issue on Mobile Safari where my HTML5 video won't return to the start once it has finished, so that when it is played again it tries to play the last second before closing once again.
I've attached a handler to the "ended" event so that it closes to the full screen once the video has finished:
$(videoID).bind("ended", function() {
$(videoID).get(0).webkitExitFullScreen();
});
Any ideas?
I should add that I've tried resetting the currentTime to 0, but this doesn't seem to work.
Ended up that I had stupidly put currentTime=0 after exiting full screen when it needed to be before. i.e.
$(videoID).bind("ended", function() {
$(videoID).get(0).currentTime = 0;
$(videoID).get(0).webkitExitFullScreen();
});

Stop YTplayer video when link is clicked ... or just pause it?

Site plays a full screen video with sound. The site was designed to play the video without sound. When a link is clicked the other portions of the site appear on top of the video.
Unfortunately the client has insisted on having sound, so when something is clicked you still hear the music until the video ends. I know it is possible to have the video stop or even have it muted when a link is clicked but I cannot seem to understand where exactly to implement this. Your help is appreciated. I'm not a jquery person so I'm rather dim on this. Site uses the YTPlayer ( http://pupunzi.open-lab.com/mb-jquery-components/jquery-mb-ytplayer/ )
The site: http://www.bradfordweb.com/clients/concannon2/
The code in the page that is playing the video is:
<a id="P2" class="player" data-property="{videoURL:'http://youtu.be/OgAr2jQr3rg',containment:'#home',autoPlay:true, mute:false, loop:false, opacity:.6}"></a>
The link is:
<div class="link-home"><div class="cl-effect-8"><span>ABOUT US</span> </div></div>
I tried:
onclick="stop()"
and
stopYTP
I found the function in the jquery.mb.YTPlayer.js file:
stopYTP: function () {
var YTPlayer = this.get(0);
var controls = jQuery("#controlBar_" + YTPlayer.id);
var playBtn = controls.find(".mb_YTVPPlaypause");
playBtn.html(jQuery.mbYTPlayer.controls.play);
YTPlayer.player.stopVideo();
},
Help?
I've had this issue as well and found it in the onYouTubePlayerReady function.
Look for it in that file and do the following changes:
function onYouTubePlayerReady(playerId) {
var player=$("#"+playerId);
player.mb_setMovie();
// Remove or comment out the lines below.
// $(document).on("mousedown",function(e){
// if(e.target.tagName.toLowerCase() == "a")
// player.pauseYTP();
// });
}
That line just simply binds the mousedown event on an a tag and consequently pauses the player.
Hope that helps.

Change firefox playback bar into a onclick button for audio

SAME ISSUE! As we all know firefox and audio is a problem because of patents and such. I found this little code on the internet to play my sounfile.
I would like to play multiple files instead of just one while having the display bar not show up in the browser
you can change the player width to 0 but than the user can not click the play button :P
Is there a way of possibly having the sound play on click of a link or button.
Please note. Do not give me codes that have no compatibility outside chrome and ie.
HTML
<audio id="audioplayer" preload controls loop style="width:400px;">
<source src="sounds/sound1.mp3">
<source src="sounds/sound1.caf">
</audio>
Javascript
<script type="text/javascript">
var audioTag = document.createElement('audio');
if (!(!!(audioTag.canPlayType) && ("no" != audioTag.canPlayType("audio/mpeg")) && ("" != audioTag.canPlayType("audio/mpeg")))) {
AudioPlayer.embed("audioplayer", {soundFile: "sounds/sound1.mp3"});
}
</script>
RECAP:
Have the sound play on a button or link click.
Have multiple sounds available to play (not just one)
Compatibility with firefox
non visible soundbar.
Still learning myself. But this is a button, with a script to play an audio file. (part of my own solution) Plays only 1 sound at a time, but doesn't show anything about it.
You could also make a funtion like this, without setting the src, using the pause() command.
currenttime is used to change the part where the audio file was.
Sound play button<br>
<script>
sound = new Audio();
function playSnd(soundSrc) {
sound.src = soundSrc;
sound.play();
}
</script>

<audio> element not working at all in Safari

I'm trying to add music to a virtual tour application I'm writing in JS and jQuery and so far my code, shown below, works great in Chrome, FF, IE9, and Opera. But in Safari 5.1.7, which is the newest you can get for a Windows machine, it just doesn't work... Through my research of the problem, I know that Safari doesn't autoplay music and it has to be started manually, but when I click the play button, nothing happens. And, btw, the code inspector in Safari shows that my audio tag is there with the music sources so I don't think it has anything to do with the DOM. Any ideas on my problem?
HTML:
<div id="musicBtns">
<p>Music:</p>
<p id="musicPlayBtn">Play</p>
<p>/</p>
<p id="musicPauseBtn">Pause</p>
</div>
My music object in JS:
var Music =
{
musicBtns: $('#musicBtns'),
playBtn: $('#musicPlayBtn'),
pauseBtn: $('#musicPauseBtn'),
isPlaying: false,
init: function()
{
if(!music.includeMusic) // If the client didn't want any music in the tour app, remove the music btns from the DOM
{
this.musicBtns.remove();
}
else // Else, setup the audio element
{
var parent = this;
var audio = $('<audio loop="true">');
if(music.autoplay)
{
audio.attr('autoplay', 'autoplay');
this.isPlaying = true;
this.togglePlayPause();
}
// Add a source elements and appends them to the audio element
this.addSource(audio, music.ogg); // 'music.ogg' is a reference to the path in a JSON file
this.addSource(audio, music.mp3);
this.musicBtns.append(audio);
// Add event listeners for the play/pause btns
this.playBtn.click(function()
{
audio.trigger('play');
parent.isPlaying = true;
parent.togglePlayPause();
});
this.pauseBtn.click(function()
{
audio.trigger('pause');
parent.isPlaying = false;
parent.togglePlayPause();
});
}
},
addSource: function(el, path)
{
el.append($('<source>').attr('src', path));
},
// Add or remove classes depending on the state of play/pause
togglePlayPause: function()
{
if(this.isPlaying)
{
this.playBtn.addClass('underline');
this.pauseBtn.removeClass('underline');
}
else
{
this.playBtn.removeClass('underline');
this.pauseBtn.addClass('underline');
}
}
}
Edit: Could this be a bug in Safari?
So the problem turned out to be with QuickTime. I guess I must of deleted it off of my machine awhile back because I didn't think I needed it. After I re-installed QuickTime, Safari plays music using the audio tag with no problem. Autoplay even works too.
Kinda funny how the champion of native HTML5 audio/video support, doesn't support HTML5 audio/video without a plugin...
In HTML5 video on Safari I had an issue with this where I had to "load" (which, I think, either starts the buffer or loads the whole thing) before I set it to play. This seemed to actually make things work in a few webkit browsers. So something like:
var a = new Audio("file.mp3");
a.load();
a.play();
Worth a try

Categories