Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start playing SoundCloud audio stream in iOS by using a pre-buffer

Tags:

ios

soundcloud

Using the following code example from SoundCloud Developers page, the AVAudioPlayer will start playing after the SCRequest response has been received. Depending on the size of the requested file, this might take some time.

Does the iOS SoundCloud API offer a pre-buffer solution, so that it would be possible to start playing audio before all data has been received or do I need to implement a own solution with help of NSURLConnection in order to achieve this?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSDictionary *track = [self.tracks objectAtIndex:indexPath.row];
     NSString *streamURL = [track objectForKey:@"stream_url"];
     SCAccount *account = [SCSoundCloud account];

     [SCRequest performMethod:SCRequestMethodGET
                   onResource:[NSURL URLWithString:streamURL]
              usingParameters:nil
                  withAccount:account
       sendingProgressHandler:nil
              responseHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
             NSError *playerError;
             player = [[AVAudioPlayer alloc] initWithData:data error:&playerError];
             [player prepareToPlay];
             [player play];
         }];
}  
like image 643
Björn Avatar asked Feb 13 '13 07:02

Björn


2 Answers

To stream tracks from SoundCloud, all you need to do is pass the URL to AVPlayer with the client id:

   NSString *urlString = [NSString stringWithFormat:@"%@?client_id=%@", track.streamURL, self.clientId];
player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlString]];
[player play];

It will take a bit of extra work to make it a progressive download. Hope that helps.

like image 188
Clive Jefferies Avatar answered Nov 10 '22 02:11

Clive Jefferies


afaik there is no pre-buffer solution at the moment. If you want to contribute to our SoundCloud API we'd love to review a Pull Request from you regarding this feature.

This will probably affect CocoaSoundCloudAPI and OAuth2Client.

Happy Coding!

like image 25
nerdsRob Avatar answered Nov 10 '22 01:11

nerdsRob