Better dealing with autoplay
This commit is contained in:
parent
8347a95867
commit
19f4316e65
3 changed files with 17 additions and 21 deletions
|
@ -1,31 +1,29 @@
|
|||
var playing = false;
|
||||
var control = document.getElementById('control');
|
||||
var playing = false;
|
||||
var control = document.getElementById('control');
|
||||
var background = document.getElementById('background');
|
||||
|
||||
var sound = new Howl({
|
||||
var sound = new Howl({
|
||||
urls: ['assets/audio/africa.ogg', 'assets/audio/africa.m4a'],
|
||||
autoplay: true,
|
||||
loop: true
|
||||
loop: true,
|
||||
onload: function() {
|
||||
setTimeout(makeItPlay, 5);
|
||||
}
|
||||
});
|
||||
|
||||
function toggle() {
|
||||
if(playing) {
|
||||
sound.pause();
|
||||
playing = false;
|
||||
playing = false;
|
||||
control.className = 'icon-play';
|
||||
} else {
|
||||
sound.play();
|
||||
playing = true;
|
||||
playing = true;
|
||||
control.className = 'icon-pause';
|
||||
}
|
||||
}
|
||||
|
||||
function load() {
|
||||
makeItRain();
|
||||
if(!isMobile.any()) {
|
||||
playing = true;
|
||||
control.style.visibility = 'hidden';
|
||||
}
|
||||
}
|
||||
|
||||
function makeItRain() {
|
||||
|
@ -39,12 +37,9 @@ function makeItRain() {
|
|||
background.crossOrigin = 'anonymous';
|
||||
}
|
||||
|
||||
var isMobile = {
|
||||
Android: function() { return navigator.userAgent.match(/Android/i); },
|
||||
BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); },
|
||||
iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); },
|
||||
Opera: function() { return navigator.userAgent.match(/Opera Mini/i); },
|
||||
Windows: function() { return navigator.userAgent.match(/IEMobile/i); },
|
||||
any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); }
|
||||
};
|
||||
|
||||
var makeItPlay = function() {
|
||||
if(sound.pos() === 0) {
|
||||
playing = false;
|
||||
control.style.visibility = 'visible';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ html, body, img, #container {
|
|||
}
|
||||
|
||||
#control {
|
||||
visibility: hidden;
|
||||
display: inline-block;
|
||||
border-radius: 10px;
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@
|
|||
<img id="background" src="" />
|
||||
|
||||
<div id="container">
|
||||
<i id="control" class="icon-pause" onclick="javascript:toggle();"></i>
|
||||
<i id="control" class="icon-play" onclick="javascript:toggle();"></i>
|
||||
</div>
|
||||
|
||||
<script src="//cdnjs.cloudflare.com/ajax/libs/howler/1.1.17/howler.min.js"></script>
|
||||
|
|
Loading…
Reference in a new issue