Downloading cover art URL from Spotify and key-value observing

别说谁变了你拦得住时间么 提交于 2019-12-08 06:01:09

问题


I'm displaying a list of Spotify tracks in a table view in my application, and I'd like to download the cover art for the albums that these tracks belong to.

I'm logged in to CocoaLibSpotify, and I can play all the tracks, so that part is fine. What I can't seem to get to work is the download of the cover art. I follow the code of the SimplePlayer project, with the exception that I store the SPTrack instances in a mutable dictionary (tracksDownladingForCoverArt), since there could be many tracks downloading cover art.

So, this is what I do:

[[SPSession sharedSession] trackForURL:nativeSpotifyURL callback:^(SPTrack *track) {
    if (track != nil) {

        [track addObserver:self forKeyPath:@"album.cover.spotifyURL" options:0 context:NULL];
        self.tracksDownloadingForCoverArt[nativeSpotifyURL] = track;

        [SPAsyncLoading waitUntilLoaded:track timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *tracks, NSArray *notLoadedTracks) {
            if ([tracks count] > 0) {
                // I don't believe I need to do anything here
            }
        }];
    }
}];

The observeValueForKeyPath:ofObject:change:context: is called once, with an old and new value of nil, but after that, nothing. However, if I follow SimplePlayer to the letter (i.e. have self.currentTrack instead of a dictionary) and register for the key path @"self.currentTrack.album.cover.spotifyURL", the observerValue... method is called twice, once with nil and once with an actual value.

What am I missing? Oh, and I register for the spotifyURL rather than the image, so I can cache the image. That way, when the user quits the app and returns later, not all the images have to be reloaded.


回答1:


This is intended behaviour — cover art isn't loaded unless you explicitly ask for it to keep data usage down. To load all the cover art, you could do this…

NSArray *tracksToGetArtFor = …; // Get some tracks;
[SPAsyncLoading waitUntilLoaded:tracks timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedTracks, NSArray *notLoadedTracks) {

    NSArray *coverArts = [tracks valueForKey:@"album.cover"]; // Get an array of the track's covers

    [SPAsyncLoading waitUntilLoaded:coverArts timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedCovers, NSArray *notLoadedCovers) {
        NSLog(@"Number of covers that're loaded: %@:", @(loadedCovers.count));
    }];
]};

You'll likely want a larger timeout for loading a lot of covers, since they can be fairly big.

Another approach if you don't want to use SPAsyncLoading for the covers is to load the tracks using SPAsyncLoading then KVO on each track's album.cover property. To force all the covers to start loading, you need to call the the -startLoading method of each cover. For example:

NSArray *tracksToGetArtFor = …; // Get some tracks;
[SPAsyncLoading waitUntilLoaded:tracks timeout:kSPAsyncLoadingDefaultTimeout then:^(NSArray *loadedTracks, NSArray *notLoadedTracks) {

    NSArray *coverArts = [tracks valueForKey:@"album.cover"]; // Get an array of the track's covers

    // Trigger the -startLoading method of each cover.
    [coverArts makeObjectsPerformSelector:@selector(startLoading)];

]};


来源:https://stackoverflow.com/questions/14287370/downloading-cover-art-url-from-spotify-and-key-value-observing

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