简体   繁体   English

通过使用预缓冲区开始在iOS中播放SoundCloud音频流

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

Using the following code example from SoundCloud Developers page, the AVAudioPlayer will start playing after the SCRequest response has been received. 使用SoundCloud开发人员页面上的以下代码示例,在收到SCRequest响应后,AVAudioPlayer将开始播放。 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? iOS SoundCloud API是否提供了预缓冲解决方案,以便可以在接收到所有数据之前开始播放音频,或者我是否需要在NSURLConnection的帮助下实现自己的解决方案才能实现?

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

To stream tracks from SoundCloud, all you need to do is pass the URL to AVPlayer with the client id: 要从SoundCloud流式传输曲目,您所需要做的就是将URL传递给具有客户端ID的AVPlayer:

   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. 希望能有所帮助。

afaik there is no pre-buffer solution at the moment. afaik目前没有预缓冲解决方案。 If you want to contribute to our SoundCloud API we'd love to review a Pull Request from you regarding this feature. 如果您想为我们的SoundCloud API做出贡献,我们很乐意查看您关于此功能的请求请求。

This will probably affect CocoaSoundCloudAPI and OAuth2Client . 这可能会影响CocoaSoundCloudAPIOAuth2Client

Happy Coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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