-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On mobile devices that support HTML5 - IOS specifically, an <audio> tag element must exist to actually play a song loaded from a server to prevent "Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture." #149
base: master
Are you sure you want to change the base?
On mobile devices that support HTML5 - IOS specifically, an <audio> tag element must exist to actually play a song loaded from a server to prevent "Failed to execute 'play' on 'HTMLMediaElement': API can only be initiated by a user gesture." #149
Conversation
…yed instantly when loading it from the server without any error - on mobile devices Without clicking twice on the element
Hi, Iv'e tried using the code you wrote here inside my react application (AKA creating the audio element as soon as the application loads and before I play the first sound). |
On top of updating SM2 to help prevent this issue, we played a blank.mp3 file and once we want to play the actual desired song, we pause, switch the blank.mp3 sound's URL with the new desired URL, and play again. |
Hope this help your issue. Cheers! |
Unfortunately it does not work for me. I'm trying to use the native Audio javascript component, such as:
But it still prompts the error message that the user must initiate the operation with a gesture... (BTW it seems there's nobody to accept the PR :) ) |
You shouldn't use it as such, but you should initialise soundManager and it creates an HTMLAudioElement manually and place it in the DOM. |
Sorry for the late follow-up on this, I'm curious about this PR but hesitant to simply merge it - something doesn't sound quite right about the user experience. It sounds like you are saying the user needs to tap / click / act twice to get audio to start, which is undesirable. However, SM2 only requires one click/tap to start audio. You can create and play a sound from a single user action in SM2, but it must be synchronous - no It seems you're suggesting that if an My experience is that on mobile, user action is always required for playback to start - and on iOS, there is no difference whether the |
Dear Scott,
Unfortunately, after going in depth in the SoundManager2 api by using it in
our Web App on https://play.anghami.com, we found that there's a limitation
to play audio on mobile, due to the error 'Call to play must happen from a
user gesture event'.
We use a single page app framework AngularJs, and we initiate the player
through a directive that is a custom element to manipulate that element and
add logic to it.
Let's take this scenario where I click on a track from a list. The click
method handles loading the track hash from the server and building the URL
on the client, and issuing a click event to play the track by using
soundManager.createSound method. In fact, this didn't work, on both Android
and IOS devices.
Googled around for some solutions and one have stated that if you try to
play a blank sound, wait for the url to load asynchronously, then switch
the src with the new URL, it would work.
However, the warning in the console persisted.
This PR is a solution for this problem that has been tested to work on
mobile devices. On top of that, if there's a request to the server, the
developer should call play with an empty URL so he can get over the browser
limitation, and once the full URL is built, he switches the src with the
new URL and play without any other problem.
I truly believe that this is the solution, if there was test cases, we
would have run them and issued an asynchronous to play a song, we would
have faced the same solution.
|
It's a perfect solution for this. Few days ago, at Google IO, they talked
about the state of HTML Audio and about those concerns. A member of our
team went and he discussed with us the changes that are going to happen and
one of them is Autoplay on mobile and other concerns such as the above.
…On Sun, May 21, 2017 at 7:02 PM, Mohammad Sidani ***@***.***> wrote:
Dear Scott,
Unfortunately, after going in depth in the SoundManager2 api by using it
in our Web App on https://play.anghami.com, we found that there's a
limitation to play audio on mobile, due to the error 'Call to play must
happen from a user gesture event'.
We use a single page app framework AngularJs, and we initiate the player
through a directive that is a custom element to manipulate that element and
add logic to it.
Let's take this scenario where I click on a track from a list. The click
method handles loading the track hash from the server and building the URL
on the client, and issuing a click event to play the track by using
soundManager.createSound method. In fact, this didn't work, on both Android
and IOS devices.
Googled around for some solutions and one have stated that if you try to
play a blank sound, wait for the url to load asynchronously, then switch
the src with the new URL, it would work.
However, the warning in the console persisted.
This PR is a solution for this problem that has been tested to work on
mobile devices. On top of that, if there's a request to the server, the
developer should call play with an empty URL so he can get over the browser
limitation, and once the full URL is built, he switches the src with the
new URL and play without any other problem.
I truly believe that this is the solution, if there was test cases, we
would have run them and issued an asynchronous to play a song, we would
have faced the same solution.
--
*Mohammad K. Sidani*
Developer - Mobile, Web, Entertainment
B.S Computer Science
*E-mail:* [email protected]
*Tel:* +96176834807
Beirut, Lebanon - Raas El Nabeh - Mohammad El hout St. - El Kibbi Bldg. -
G.F
<https://github.com/mhdSid>
<https://lb.linkedin.com/in/mohammad-sidani-6b9a32aa>
|
@Lazey5 Check this out: |
@scottschiller At Anghami, we heavily use SoundManager2 in order to give a pleasant music experience on the web. However, on some browsers such as Instagram browser, iOS, android, PS4, we have faced a lot of issues with SoundManager2 in order to fetch songs from the server and play. Please I would like you to check it out and give me a feedback. |
Thanks for the update, sorry for the random spam comments above - reported. I still haven't worked this PR in, as you should be able to initiate a If you're trying to work around asynchonous fetches to get the MP3 (for example) you want to play and that's why you must play a blank .mp3 first, my $.02 is that you're playing with fire in hoping that - for example, playing a silent sound and then having it Notwithstanding, you've indicated you found a solution and SM2 should also handle this - your point about the SM2 has existed for over 10 years now - not to say that means it's good by default, but it has gone through a lot of testing and has dealt with obscure browser bugs along the way. Mobile is picking up the pace, and desktop is starting to adapt mobile's auto-play blocking as well. I have intent to move SM2 to 100% HTML5 soon, though it won't matter much with regards to retiring of Flash or Chrome's stated deadline for the new auto-play restrictions. Ideally, developers aren't running afoul of those rules unless there's a very specific exception like in your async case. |
Mobile browsers have always had a problem when it comes to playing audio files in the browser.
Both Chrome & Safari state that a user interaction event - click event - must occur for an audio file to be in the playing state.
Any HTTP request should occur in the same 'stack frame' of the click event.
When using Chrome on Android, we tried to request a URL from the server, to give it to SoundManager that creates an tag dynamically and feed its source with the given URL. The song didn't play.
We found a solution to this problem that is to play a blank mp3 file locally, get the song URL from the server, then replace the currently playing song with the new URL.
Plus, the tag must not be created dynamically.