How can I access track/album art from local music from the Spotify API?

…衆ロ難τιáo~ 提交于 2019-12-11 19:48:40

问题


I am in the process of building an app using the Spotify API as a learning project for myself. The purpose of the app is to provide a web based remote control for Spotify so I can control a running instance of Spotify on my desktop using a phone or tablet. The Spotify app talks to my Python web server using websockets so I've gotten the core remote control functionality working fine, but I am running into a separate issue.

In the Spotify app, when it encounters a local file (player.track.local = true), player.track.image returns an empty string. When playing a Spotify hosted track, player.track.image always returns a URI in the form of "spotify:image:...". I know that the Spotify Desktop app can see the album art because it displays in the player on the bottom left. I just can't access it for use in my app.

Is there any way to retrieve this art (or failing that a path to the local file so I can go get it myself from the Python server?) Thanks.

-Jeremy


回答1:


In short: no - local files' album art is not stored in the backend and the client doesn't expose local data.




回答2:


I figured that was the case.

Since I was running into other issues with the new API (such as no longer allowing access to now playing info) i decided to stick with the old API and I was able to make it work from there. I took advantage of the fact that playlists generate a cover image from their contents, so I just did the following:

(I updated this to reflect some simplification I did after spending some time learning more about arrays today)

function getLocalArt() {
    sp.core.library.createPlaylist("tempPL");
    var mosaicURI;
    var trackURI = player.track.uri;
    sp.core.library.getPlaylistsUri().forEach(function (p) {
        if (p.name == "tempPL") {
            p.add(trackURI);
            mosaicURI = p.cover;
            sp.core.library.removePlaylist(p.uri);
        }
    });
    return mosaicURI;  
}

This works rather well for my purposes and returns a string that looks like this:

spotify:mosaic:localfileimage%3AZ%253A%255CiTunes%255CiTunes%2520Media%255CMusic%255CBroken%2520Social%2520Scene%255CEarCande.com%255CAll%2520To%2520All%2520(Skeet%2520Skeet%2520Remix).mp3

This has a side benefit of providing me with the full path to the track in the file system, which I can pass to my server so I can use that to extract album art for display in my web remote. I understand that the use of sp.core is verboten for apps being distributed to the public, but it works for my little project. Thanks.



来源:https://stackoverflow.com/questions/19894092/how-can-i-access-track-album-art-from-local-music-from-the-spotify-api

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!