Play Audio - iOS Plays No Sound - Delay in Sound Output

Asked

Viewed 63 times

0

I have this function to load, remove, startar and pause the audio.

media = (function(){
    return{
        create : function(name, src){
            mediaData[name] = new Audio();
            mediaData[name].src = baseURL+'/media/'+src;
            mediaData[name].volume = .5;
        },
        remove : function(name){
            if(mediaData[name]){
                delete mediaData[name];
            }
        },
        play : function(name){
            return mediaData[name].play();
            if(!Modernizr.touch)
                mediaData[name].currentTime = 1;
        },
        stop : function(name){
            mediaData[name].pause();
            if(!Modernizr.touch)
                mediaData[name].currentTime = 0;
        },
        replay : function(name){
            if(!Modernizr.touch)
                mediaData[name].currentTime = 0;
        }
    }
})();

But in the Safari of iPad I’m having problems. Some audios load and work, others don’t play. And other browsers work normally.

I wonder what it might be ?

An example of what I call audio via JS.

setTimeout(function() {
  media.play("chime-01"); /////////////// FUNCIONA
  setTimeout(function() {
    $("div#gift").addClass('anime');
    var randomGift = Math.floor((Math.random() * totalGifts) + 1);
    var gift = $('div#gift > .gifts > div:nth-child(' + randomGift + ')');
    gift.addClass('active');
    setTimeout(function() {
      gift.addClass('rotate');
      //Frase do objeto
      var audio = gift.data('audio');
      setTimeout(function() {
        media.play(audio); ////////////// NÃO FUNCIONA
      }, 1400);
    }, 2000);
    media.play("tada");
  }, 100);
}, 250);

In the code above I commented the line that audio works and the line that does not work on iOS - Safari - iPad.

But in other browsers work both lines. So, before anyone claims, the audio path is right, the variables with their values too.


I was reading in the Safari documentation and I found this:

In Safari on iOS (for all Devices, including iPad), Where the user may be on a Cellular network and be Charged per data Unit, Preload and autoplay are disabled. No data is Loaded until the user initiates it.

Translating:

In Safari on iOS (for all devices including iPad), where the user can be on a cellular network and be charged per data unit (3G, 4G), preloading and autoplay are disabled. No data is loaded until the user starts it.

And now ? There’s no way ?

  • 1

    Return won’t let the code get to "if" to check modernizr. Have you noticed that?

  • The return of play ? Yeah, I noticed. I took it off, but it didn’t work.

  • I also noticed that in the browser the sound works late.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.