Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soundcloud iOS API - playing sound from link

I want to play a public sound in my app using the Soundcloud API. After some searchs around the web, I find a way to do it, but I can't make my code works. Here's the code I tried to play a sound :

    NSString *publicTrackUrlString = @"https://soundcloud.com/miroslav-osipovic/the-cat-empire-the-lost-song";
NSString *urlString = [NSString stringWithFormat:@"%@?client_id=64a52bb31abd2ec73f8adda86358cfbf", publicTrackUrlString];
[SCRequest performMethod:SCRequestMethodGET
              onResource:[NSURL URLWithString:urlString]
         usingParameters:nil
             withAccount:nil
  sendingProgressHandler:nil
         responseHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
             NSError *playerError;
             player = [[AVAudioPlayer alloc] initWithData:data error:&playerError];
             [player prepareToPlay];
             [player play];
         }];
like image 781
Boris88 Avatar asked Jan 17 '14 03:01

Boris88


1 Answers

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];

You can get the track ID from the resolve resource: http://api.soundcloud.com/resolve.json?url=http://soundcloud.com/miroslav-osipovic/the-cat-empire-the-lost-song&client_id=YOUR_CLIENT_ID.

like image 143
jonahb Avatar answered Oct 01 '22 23:10

jonahb