Xpistoforos
Xpistoforos

Reputation: 43

iOS / Soundcloud API - Downloading track from Soundcloud API without stream_url request

Part of my iOS app is playing songs downloaded from Soundcloud with the help of their API. However, the songs are fully downloaded then played, not streamed. When the GET request is sent using the stream_url, the request takes extremely long to load into an AVAudioPlayer (I guess because of the 128kb/s cap on a track that is over an hour long), and the request using download_url does not work, most likely because downloading on the track is setup to only be accessible through a Facebook Band page.

I was curious if there is a faster way to download a track with these two options eliminated. Otherwise, is there a way to start streaming the song while the rest of the song loads into the player. For example, can I load the first 5 minutes of a track into a temporary player and then when the other player fully loads, have it take over the playing? The Soundcloud tracks being accessed are all from a friend, so I can ask him to change account options if necessary.

Upvotes: 1

Views: 1787

Answers (2)

santibernaldo
santibernaldo

Reputation: 835

I have found a really good answer here to speed up the playing using AVPlayer.

Soundcloud iOS API - playing sound from link

I've tried already and the response is really good!

The code is:

NSString *trackID = @"100026228";
NSString *clientID = @"YOUR_CLIENT_ID";
NSURL *trackURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.soundcloud.com/tracks/%@/stream?client_id=%@", trackID, clientID]];

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:trackURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // self.player is strong property
    self.player = [[AVAudioPlayer alloc] initWithData:data error:nil];
    [self.player play];
}];

[task resume];

Upvotes: 3

Twinkle
Twinkle

Reputation: 430

you need to use streaming technique instead of AVAudioPlayer . You can use AudioStreamer to allow playing audio while the loading. this solve my problem

Upvotes: 2

Related Questions