Question

I'm developing a script for my website that randomizes my favorite songs and I want to let a user listen to the song by just clicking it.

Right now, I'm generating this kind of links using local spotify: URLs

<a href='spotify:search:track:"+ trackVal +"+artist:"+ artistVal +"'>"+ trackVal +"</a>

Unfortunately this is not working when the user doesn't have Spotify installed on his device. Is there a way to detect/foresee if the link will work or not (just for fallback purposes)?

Was it helpful?

Solution

One way of detecting a spotify installation: Spotify runs an http server (1234.spotilocal.com:12345 -> 127.0.0.1:12345) on the client computer if you have spotify installed, to enable javascript interaction. To detect if spotify is installed, try to connect to the local server to see if it exists. However, this does not guarantee that spotify: links will work as it may be disabled by the user, but it's a lot easier than detecting a custom protocol handler. Finally, you might run into cross-domain issues, I'm not sure if the local server supports JSONP... If you want to go down this long road, here's a good read.

To stay simple still get a clean fallback, consider using http://open.spotify.com/track/(yourTrackNo) urls instead. The open.spotify.com page will detect if spotify is installed and prompt the user to install and/or listen online if its not. To get the url (yourTrackNo) of your song if you don't already have it (i don't know how you generate your links, manually?), you have to use the Spotify Metadata Api, now deprecated in favour of Spotify WebApi

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top