spotify / web-playback-sdk

This issue tracker is no longer used. Join us in the Spotify for Developers forum for support with the Spotify Web Playback SDK ➡️ https://community.spotify.com/t5/Spotify-for-Developers/bd-p/Spotify_Developer
106 stars 7 forks source link

play() can only be initiated by a user gesture (Android) #5

Open Rudloff opened 6 years ago

Rudloff commented 6 years ago

Issue found on 2018-04-01.

Scope(s):

streaming user-read-birthdate user-read-email user-read-private user-library-read

Steps to reproduce:

I'm using Chrome 63 on Android 6.0.

  1. Initialize the playback SDK.
  2. Inside a click event listener, call https://api.spotify.com/v1/me/player/play with the current app as device_id.

Expected behaviour:

The track starts playing.

Actual behaviour:

Chrome throws this exception:

index.js:2 Uncaught (in promise) DOMException: play() can only be initiated by a user gesture.
(anonymous) @ index.js:2
Promise (async)
(anonymous) @ index.js:2
Promise rejected (async)
o._handleLoadedMetadata @ index.js:2
i @ index.js:2
r.emitEventSync @ index.js:2
r.emitSync @ index.js:2
o._onLoadedMetadata @ index.js:2

(Because the play action is indeed started by the API and not directly by the click event.)

But subsequent calls to player.resume() work correctly if they are initiated by a click event.

So as a workaround I had to use this after the API call:

setTimeout(function () {
    player.resume();
}, 1000);

(I know mobile browsers are not officially supported but everything else works fine in Android Chrome.)

hughrawlinson commented 6 years ago

Thanks for the report @Rudloff! As you mentioned, mobile browsers aren't officially supported, but I'll take this to the team and see if there's a way to catch that error and handle it better. Do you have an example app that reproduces this? It'd be really helpful for us.

Rudloff commented 6 years ago

Thanks for reading my report!

This app reproduces the issue (with the workaround I mentioned but you can still see the error in the browser console). (You can have a look at the code here.)

jespertheend commented 6 years ago

This might be an issue for android now. But it might be an issue for chrome desktop soon as well: https://github.com/spotify/web-playback-sdk/issues/33#issuecomment-431498472

ajaichemmanam commented 5 years ago

This may help you.

int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT > 16) {    
WebViewWithJavaScript.getSettings().setMediaPlaybackRequiresUserGesture(false);
}