简体   繁体   中英

Spotify API for ios: download , save , access tracks from ios spotify api

I would like to know whether downloading tracks to the iphone / ipod / ipad devices is possible using the spotify ios api (CocoaLibSpotify IOS Library). If so I also need to access them and play anytime I want. I will be using them in my app later.

I think we can check the offline status of the track that is being played by using sp_track_offline_status. But I am unable to get through that.

Any sample code snippet would be of great help.

Simplifying the requirement : download and save the spotify tracks into iOS devices

As an update, in response to iKenndac's answer, the offlineStatus property has become '1' which means that the playist is synchronised for local storage.

SP_PLAYLIST_OFFLINE_STATUS_YES = 1, ///< Playlist is synchronized to local storage

1.Does that mean all the tracks in the playlist are downloaded?

I dont think so. because there are other states as follows:

typedef enum sp_playlist_offline_status {
  SP_PLAYLIST_OFFLINE_STATUS_NO          = 0, ///< Playlist is not offline enabled
  SP_PLAYLIST_OFFLINE_STATUS_YES         = 1, ///< Playlist is synchronized to local storage
  SP_PLAYLIST_OFFLINE_STATUS_DOWNLOADING = 2, ///< This playlist is currently downloading. Only one playlist can be in this state any given time
  SP_PLAYLIST_OFFLINE_STATUS_WAITING     = 3, ///< Playlist is queued for download
} sp_playlist_offline_status;

I had never got the offlineStatus either 2 or 3.

  1. Also my offlineDownloadProgress property is always showing me '0'.
    It has to increase from '0' to '1', if I am not wrong.

CODE snippet:

[SPAsyncLoading waitUntilLoaded:playList 
                        timeout:kSPAsyncLoadingDefaultTimeout 
                           then:^(NSArray *loadedItems, NSArray *notLoadedItems) {    
    playList.markedForOfflinePlayback = YES;
    currentPlaylist = playList;
    statusTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(checkOfflineStatus) userInfo:nil repeats:YES];

I am checking the status as follows:

-(void)checkOfflineStatus {

  NSLog(@"playlist offline progress is: %f",currentPlaylist.offlineDownloadProgress);

  NSLog(@"offline status: %d",currentPlaylist.offlineStatus);

}

The output looks like this:

2012-07-06 20:34:05.891 Simple Player[6571:10703] playlist offline progress is: 0.000000

2012-07-06 20:34:05.892 Simple Player[6571:10703] offline status: 1

2012-07-06 20:34:06.039 Simple Player[6571:10703] playlist offline progress is: 0.000000

2012-07-06 20:34:06.039 Simple Player[6571:10703] offline status: 1

What can I do next?

You can't simply download Spotify tracks as, say, MP3 files or whatever for independent playback.

However, you can have the library cache tracks for offline playback later, as long as the user stays logged into CocoaLibSpotify, just like the iOS Spotify client does.

SPPlaylist has the markedForOfflinePlayback property for enabling offline access, then the offlineDownloadProgress and offlineStatus properties of the playlist give more information.

CocoaLibSpotify comes with a Mac sample application that caches playlists for offline playback to see how it works. The API is available and the same on both Mac OS X and iOS versions of CocoaLibSpotify.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM