Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Music not play on mobile Safari #1

Open
1 of 7 tasks
anbu93 opened this issue Aug 22, 2020 · 2 comments
Open
1 of 7 tasks

Music not play on mobile Safari #1

anbu93 opened this issue Aug 22, 2020 · 2 comments

Comments

@anbu93
Copy link

anbu93 commented Aug 22, 2020

Issue details

com.badlogic.gdx.audio.Music not play on:

  • MacOS: Safari broswer
  • iPhone: mobile Safari
  • iPad: mobile Safari and Chrome

On mac this error in console: Unhandled Promise Rejection: NotAllowedError.
But com.badlogic.gdx.audio.Sound works normally.

Reproduction steps/code

Play any music on Mobile safari.

Version of LibGDX and/or relevant dependencies

implementation 'com.github.MrStahlfelge.gdx-backends:gdx-backend-gwt:1.910.0'

Solve

Recreate Gdx.audio after user clicked anywhere.

public class HtmlLauncher extends GwtApplication {
         // USE THIS CODE FOR A FIXED SIZE APPLICATION
        @Override
        public GwtApplicationConfiguration getConfig () {
                GwtApplicationConfiguration cfg = new GwtApplicationConfiguration(1280, 720);
                // UNCOMMENT THIS CODE FOR MUSIC WORK
                cfg.disableAudio = true;
                // END OF CODE FOR MUSIC WORK
                return cfg;
        }
        // END CODE FOR FIXED SIZE APPLICATION

        public void recreateAudio() {
                Gdx.audio = new GwtAudio();
        }

Call this method from core code with interface or something.

Please select the affected platforms

  • Android
  • iOS (robovm)
  • iOS (MOE)
  • HTML/GWT
  • Windows
  • Linux
  • MacOS
@MrStahlfelge
Copy link
Owner

Do you start the background music immediately or after first user interaction?

@MrStahlfelge
Copy link
Owner

Generally, playing music on game start is problematic on html5. Chrome and Firefox allow this if the user visited the webpage regularly, but you can't rely on it. That's why you should not play music on your start screen.

I gave it a try. On my Mac Safari, music never started on game start, but it worked when played a little later.
On my iOS 13.6 iPad, it did not play at all at first. I needed to set up debugging to investigate it, for this I needed to update the GWT plug in as well... and then the problem disappeared and music worked when played after a user interaction.
On my iOS 12 iPhone, music did play when tried some SECONDs after the game startet.
All in all, behaviour on Safari is weird and different on every device I used. I have no more ideas what to try.
Code trying to do this all automatically is here:

* The Web Audio API is blocked on many platforms until the developer triggers the first sound playback using the

Maybe you can add some logging to see what's happening for you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants