As per this tutorial https://developers.google.com/web/updates/2016/03/play-returns-promise I'm trying to implement playPromise in my code. The reason: Google chrome policy changed and now autoplay is not working out of the box anymore.
The instructions:
A play() call on an a or element now returns a Promise. If playback succeeds, the Promise is fulfilled, and if playback fails, the Promise is rejected along with an error message explaining the failure. This lets you write intuitive code like the following: var playPromise = document.querySelector('video').play();
- // In browsers that don’t yet support this functionality,
- // playPromise won’t be defined.
- if (playPromise !== undefined) {
- playPromise.then(function() {
- // Automatic playback started!
- }).catch(function(error) {
- // Automatic playback failed.
- // Show a UI element to let the user manually start playback.
- });
- }
Here's my full code in a fiddle: https://jsfiddle.net/jackbauer/k6qu70jr/14/
Here's what I tried:
- jQuery(function($) { $
- $(".parentlogomarca").hover(function() {
- if (html5audio !== undefined) {
- html5audio.then(function() { $
- $(".el-blogomarca,.el-alogomarca").toggle();
- }).catch(function(error) {
- // Automatic playback failed.
- // Show a UI element to let the user manually start playback.
- });
- }
This is the error I was getting before:
Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
And this is the error I'm getting now:
- defaultscripts.js:95 Uncaught ReferenceError: html5audio is not defined at
- at HTMLDivElement.<anonymous> (defaultscripts.js:95)
- at HTMLDivElement.handle (jquery.js:3)
- at HTMLDivElement.dispatch (jquery.js:3)
- at HTMLDivElement.r.handle (jquery.js:3)