spotify / ios-sdk

Spotify SDK for iOS
https://developer.spotify.com/documentation/ios/
659 stars 187 forks source link

Can't reconnect appRemote #51

Open cjam opened 6 years ago

cjam commented 6 years ago

I'm finding that my applications instance of the appRemote disconnects from the Spotify app when the application is backgrounded (which I know there is another issue for). However, when coming back into the foreground it seems that when I try to reconnect the remote to spotify, it fails with an error Connection attempt failed.. The way that I've been able to reproduce it is run my application (which connects to spotify using the Session manager and then subsequently using the Remote). Once the app is running, switch over to Spotify and then switch back to the App. At which point, the apps remote will be disconnected (since it was sent into the background), but upon trying to reconnect it I get an error connecting.

Here are the logs from my device:

AppRemote: Failed connection attempt with error: Error Domain=com.spotify.app-remote.transport Code=-2000 "Stream error." UserInfo={NSLocalizedDescription=Stream error., NSUnderlyingError=0x281869ce0 {Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSLocalizedRecoverySuggestion=Reconnect the transport to the Spotify app., NSLocalizedFailureReason=A stream error has occured in the transport.}

I've also tried explicitly disconnecting when my app goes into the background (thinking perhaps Spotify's remote service was holding a connection for a particular clientid/token or something).

Also should be noted, that at this point the only way I can get my app to reconnect to Spotify via remote is to close Spotify and my application and open my application back up to let the auth flow run again.

cjam commented 6 years ago

A few more details with regards to this issue as I'm slowly converging on what is needed to get around the issue. It seems that sometimes when I start my application it will connect briefly and then disconnect. Which I can see in the logs of my ios app. I have truncated the output as when my app connects it initially will go and request the child items / content from the Spotify app. After getting the quick connect/disconnect. If I close my app and reopen it, it will connect to the spotify app.

2018-11-02 09:52:00.863110-0700 MyApp[16100:5830453] AppRemote: Connecting...
2018-11-02 09:52:00.866170-0700 MyApp[16100:5830453] AppRemote: Established a connection to the Spotify app.
2018-11-02 09:52:00.943753-0700 MyApp[16100:5830453] AppRemote: Authenticated with the Spotify app.
2018-11-02 09:52:00.943816-0700 MyApp[16100:5830453] AppRemote: Established a session with the Spotify app.
2018-11-02 09:52:00.943909-0700 MyApp[16100:5830453] App Remote Connection Initiated
2018-11-02 09:52:00.982145-0700 MyApp[16100:5830453] AppRemote: Sending request: <SPTAppRemoteCallRequest: 0x283a77b10; id: 1, method: com.spotify.get_recommended_root_items>
2018-11-02 09:52:00.984398-0700 MyApp[16100:5830453] AppRemote: Received response: <SPTAppRemoteCallResponse: 0x283437300> to request: <SPTAppRemoteCallRequest: 0x283a77b10; id: 1, method: com.spotify.get_recommended_root_items>
2018-11-02 09:52:00.991407-0700 MyApp[16100:5830453] AppRemote: Sending request: <SPTAppRemoteCallRequest: 0x283a6c180; id: 2, method: com.spotify.get_children_of_item>
2018-11-02 09:52:00.991693-0700 MyApp[16100:5830453] AppRemote: Sending request: <SPTAppRemoteCallRequest: 0x283a68bd0; id: 3, method: com.spotify.get_children_of_item>
2018-11-02 09:52:00.991794-0700 MyApp[16100:5830453] AppRemote: Sending request: <SPTAppRemoteCallRequest: 0x283a6c4e0; id: 4, method: com.spotify.get_children_of_item>
2018-11-02 09:52:00.991879-0700 MyApp[16100:5830453] AppRemote: Sending request: <SPTAppRemoteCallRequest: 0x283a6c5a0; id: 5, method: com.spotify.get_children_of_item>
2018-11-02 09:52:01.001797-0700 MyApp[16100:5830453] AppRemote: Received response: <SPTAppRemoteCallResponse: 0x2834293a0> to request: <SPTAppRemoteCallRequest: 0x283a6c180; id: 2, method: com.spotify.get_children_of_item>
2018-11-02 09:52:01.003865-0700 MyApp[16100:5830453] AppRemote: Received response: <SPTAppRemoteCallResponse: 0x2834320c0> to request: <SPTAppRemoteCallRequest: 0x283a68bd0; id: 3, method: com.spotify.get_children_of_item>
2018-11-02 09:52:01.004342-0700 MyApp[16100:5830453] AppRemote: Received response: <SPTAppRemoteCallResponse: 0x283432640> to request: <SPTAppRemoteCallRequest: 0x283a6c5a0; id: 5, method: com.spotify.get_children_of_item>
### ... More requests / responses for child items
2018-11-02 09:52:01.317873-0700 MyApp[16100:5830453] AppRemote: Disconnected with error: Error Domain=com.spotify.app-remote.transport Code=-2001 "End of stream." UserInfo={NSLocalizedRecoverySuggestion=Reconnect to the Spotify app., NSLocalizedDescription=End of stream., NSLocalizedFailureReason=One of the streams has reached the end.}
2018-11-02 09:52:01.318032-0700 MyApp[16100:5830453] App Remote disconnected
cjam commented 5 years ago

Would anyone from the Spotify team be able to give any feedback as to under what circumstances the following error is thrown?

AppRemote: Failed connection attempt with error: Error Domain=com.spotify.app-remote.transport Code=-2000 "Stream error." UserInfo={NSLocalizedDescription=Stream error., NSUnderlyingError=0x282b1ebb0 {Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSLocalizedRecoverySuggestion=Reconnect the transport to the Spotify app., NSLocalizedFailureReason=A stream error has occured in the transport.}

I seem to be able to reproduce it by:

  1. opening my App (which then authenticates and connects).
  2. Switching to the Spotify App.
  3. Switching back to my app

I have an indicator that keeps track of the connection status so I can see it switch. I'm just not sure how Spotify is keeping track of the Remote app clients. Perhaps I need to disconnect or clean up some resources on my apps side?

brunomartinez16 commented 4 years ago

Hey dude, the same thing is happening to me, do you find out a solution?

ErikAGriffin commented 4 years ago

@brunomartinez16 just curious, is the Spotify app playing audio when this is happening, and still not connecting? Or is this returning to the app after audio has been stopped / paused

brunomartinez16 commented 4 years ago

I found out that the problem was when i connect to another device via spotify connect (web player or other device with spotify like sonos for example) it disconnect from my app. I believe is because spotify sdk must be running on main spotify app,otherwise it suspend the spotify app

brunomartinez16 commented 4 years ago

So when i connect it again...it connect fine but change sound back to ipad

kkarayannis commented 4 years ago

Yes is expected that when a Connect device takes over playback that AppRemote will disconnect. It is also expected that when connecting again, the playback will switch over to the local device.

As for the connection error after exiting your app, please make sure to call appRemote.disconnect() in your willResignActive method in your Application/Scene delegate, otherwise the socket stays open when the app is suspended, which causes problems when trying to use it again.

deepam1212 commented 4 years ago

Any update on this????? stuck here

lucasgismondi commented 4 years ago

Also having issues with this. Currently unable to connect to spotify unless spotify is playing music.

ErikAGriffin commented 4 years ago

@lucasgismondi I can't remember where I read this, but that is intentional / mandatory.

My understanding is this: In order for your app to connect to the Spotify app while it is in the background, the Spotify app must somehow remain active while in the background. The only way it can achieve this if it is playing audio, a functionality in iOS which allows an app to remain active while in the background. If audio is not playing, the app goes to sleep when it resigns active (goes into the background) and your app is not able to connect to the Spotify app.

lucasgismondi commented 4 years ago

@ErikAGriffin Okay thank you this makes sense. But just to clarify, there is no other way to stream songs unless you are connected to app remote? If so, the only way to stream music through my own app is to ensure Spotify is playing music, then connect to Spotify through my app. Thanks in advance!

ErikAGriffin commented 4 years ago

@lucasgismondi Yes with Spotify's current API the only way to stream music is using AppRemote. Has to do with their licensing agreements I believe. When you do the initial authorization following their developer guide the Spotify app will begin automatically playing the song whose ID you specify. I personally use a silent track I found. If your app pauses Spotify playback, Spotify will eventually sleep and the AppRemote will disconnect. To reconnect programatically you will need to reinitiate the authorization flow

lucasgismondi commented 4 years ago

@ErikAGriffin Okay great thank you for the quick response!

cjam commented 4 years ago

@lucasgismondi Yes with Spotify's current API the only way to stream music is using AppRemote. Has to do with their licensing agreements I believe. When you do the initial authorization following their developer guide the Spotify app will begin automatically playing the song whose ID you specify. I personally use a silent track I found. If your app pauses Spotify playback, Spotify will eventually sleep and the AppRemote will disconnect. To reconnect programatically you will need to reinitiate the authorization flow

What silent track did you find?

deepam1212 commented 4 years ago

@lucasgismondi Yes with Spotify's current API the only way to stream music is using AppRemote. Has to do with their licensing agreements I believe. When you do the initial authorization following their developer guide the Spotify app will begin automatically playing the song whose ID you specify. I personally use a silent track I found. If your app pauses Spotify playback, Spotify will eventually sleep and the AppRemote will disconnect. To reconnect programatically you will need to reinitiate the authorization flow

can you please provide the silent track URI?

cjam commented 4 years ago

@deepam1212 I found this one

const silentTrack = "spotify:track:7p5bQJB4XsZJEEn6Tb7EaL";
deepam1212 commented 4 years ago

@deepam1212 I found this one

@deepam1212 I found this one

const silentTrack = "spotify:track:7p5bQJB4XsZJEEn6Tb7EaL";

Perfect...... :)

MadhavanPaulrajan commented 4 years ago

Without re-authorization is there any possibilities to disconnect and reconnect app remote in spotify. Am also facing the same issue " Failed connection attempt with error...", if I receive a phone call when my app was inside spotify player. I shared my issue in detail here, (https://stackoverflow.com/questions/60060661/spotify-appremote-is-not-reconnecting-once-disconnected-in-ios)

What I did is I disconnect appRemote in app WillResignActive and reconnect in foreground state, but app fails to reconnect appRemote. Any help will be appreciated.

deepam1212 commented 4 years ago

hi can any one suggest, I want to make playlist on spotify through my app, how can I do it

tripti-dev commented 4 years ago

I have facing an issue is intermittent disconnecting of the Spotify app from the my iOS app.

didDisconnectwithError is getting called after some time with error:

AppRemote: Failed connection attempt with error: Error Domain=com.spotify.app-remote.transport Code=-2000 "Stream error." UserInfo={NSLocalizedDescription=Stream error., NSUnderlyingError=0x28104e1c0 {Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSLocalizedRecoverySuggestion=Reconnect the transport to the Spotify app., NSLocalizedFailureReason=A stream error has occured in the transport.}

raymanan11 commented 4 years ago

I'm facing the same issue. After some time, I. would keep trying to connect to the AppRemote and the Spotify music is playing meaning the app is active but this error keeps occurring. Only way to stop this is force quitting the Spotify app but that isn't practical.

EXEIdeas commented 10 months ago

AppRemote: Disconnected with error: Error Domain=com.spotify.app-remote.transport Code=-2001 "End of stream." UserInfo={NSLocalizedRecoverySuggestion=Reconnect to the Spotify app., NSLocalizedDescription=End of stream., NSLocalizedFailureReason=One of the streams has reached the end.}

Still Getting This Error...

@ErikAGriffin

What I got from your reply is when you pause the Track will disconnect Spotify after a few seconds. So to cover this, keep playing the song and if the user pauses or stops the player in your app then play a silent song to keep it connected then on replay, play the user's song again.

@lucasgismondi Yes with Spotify's current API the only way to stream music is using AppRemote. Has to do with their licensing agreements I believe. When you do the initial authorization following their developer guide the Spotify app will begin automatically playing the song whose ID you specify. I personally use a silent track I found. If your app pauses Spotify playback, Spotify will eventually sleep and the AppRemote will disconnect. To reconnect programatically you will need to reinitiate the authorization flow