Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoplay audio on mobile safari

Tags:

Before I get flamed to death, I know this doesn't work currently due to Apple's concern over downloading an audio file automatically.

However, my question is: Has anyone found a cunning workaround yet? I just want to play a start up sound on the launch of a game and currently have to wait for the user to click somewhere before I can play the audio. One of you clever chaps must have got this working by now?

like image 673
Simple Simon Avatar asked Nov 07 '12 09:11

Simple Simon


People also ask

How do I turn on autoplay on Safari Mobile?

Click on Safari > Preferences on the top menu. Click on the Websites tab at the top. Click on Auto-Play in the left column.

Does Autoplay work in Safari?

As of version 11 released in September 2017, Safari autoplay policy states that: Muted autoplay is allowed as long as users haven't blocked all autoplay in their settings. Autoplay with sound is only allowed if users configure it in their settings.


1 Answers

There is no chance to get autoplay working in mobile browsers. Android and iOS doesn't allow it and personally I think that is a feasible confinement! Imagine every second website you will open plays and ugly sound at start!

But you can make a little hack, so that the user will not remark that he currently started the audio for your application:

  1. You WILL need an user interaction to start your audio. So, your app or game maybe has a startscreen or a welcome button which needs a click to get to mainmenu or start the game. Bind to an user event (the accepted events are: "click", "touchend", "doubleclick" and "keydown") and call the load() method for your <audio>.

  2. Bind to the "canplaythrough" event of the <audio>. This event is triggered when your source is ready to play. Here you can now call play(), pause() or wait for other interactions. So the audio is ready but now you have full controll when to start or stop the sound.

  3. I also advise you to use audio sprites on mobile clients. iOS (and Android?) internally implemented audio support through a Singleton. That means that you cannot, like in desktop browser, have 10 audio elements and play differents sound at once. You can only play one file! So changing the source for different sounds takes to much time. With an audio sprite you can start your sprites when the user first interact with your website or game. Pause your sprite and when you need to play sound you have to set the currentTime to the beginning of the sprite and pause the sprite when currentTime of your file reaches the end of your sprite. There is an timeupdate Event where your can check the currentTime of your sprite.

If you are more interested I can prepare my javascript audio sprites player for you!!

like image 146
Simon Franzen Avatar answered Sep 19 '22 06:09

Simon Franzen