1

I'm using this simple script to play a background audio theme in a website, is it possible to prenvent the play on mobile?

  $(document).ready(function() {
       var audioElement = document.createElement('audio');
       audioElement.setAttribute('src', 'audio/theme.mp3');
       audioElement.setAttribute('autoplay', 'autoplay');
       audioElement.volume = 0.1;
       audioElement.loop = true;
       $.get();

       audioElement.addEventListener("load", function() {
       audioElement.play();
    }, true);
John Militer
  • 244
  • 6
  • 17
vlk
  • 1,414
  • 1
  • 13
  • 22
  • 2
    [javascript - Detecting a mobile browser](http://stackoverflow.com/questions/11381673/detecting-a-mobile-browser) – soumasandesu Apr 20 '16 at 23:25
  • There are no words to describe how annoying I find webpages that play background audio... I usually close the page immediately and continue listening to my music. – Lyall Apr 21 '16 at 08:32
  • On awwwards.com there are some of the best websites in the world using an audio background. So I think people can click on the on/off audio button before to leave the websites. This will a movie's websites and audio will be part of the navigation experience. – vlk Apr 21 '16 at 10:40

2 Answers2

0

While searching the web for my own project I found http://detectmobilebrowsers.com/.

For example, you can click on JavaScript or jQuery and get the script which you need for your site. With it, you can define when audio should or shouldn't be played.

Ashielf
  • 70
  • 10
0

I've found a simple way to do this based on screen width , maybe it isn't perfect but it works good on this website:

    var screenw = $(window).width();

     if (screenw <= 800) {  audioElement.pause(); }
     else { audioElement.play(); }

try it on http://www.nuraghesmovie.com

vlk
  • 1,414
  • 1
  • 13
  • 22