Playing a SoundCloud file in IOS app

帅比萌擦擦* 提交于 2019-12-04 09:53:27

问题


Using the quick start guide provided would like to play a SoundCloud track within IOS app.

The code provided to play the stream is here:

SCAccount *account = [SCSoundCloud account];

    [SCRequest performMethod:SCRequestMethodGET
                  onResource:[NSURL URLWithString:audioFile]
             usingParameters:nil
                 withAccount:account
      sendingProgressHandler:nil
             responseHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                 NSError *playerError;
                 audioPlayer = [[AVAudioPlayer alloc] initWithData:data error:&playerError];
                 audioPlayer.numberOfLoops = 0;

                 [audioPlayer prepareToPlay];
                 [audioPlayer play];
             }];

Problem: is there a method to play a SoundCloud file without logging in to your SoundCloud account first? We hope all users consider using SoundCloud - however for usability if would be useful if new users could listen to a number of tracks before.


回答1:


For public access with the IOS Soundcloud SDK you will need to add the Soundcloud clientID as a parameter to your request string:

NSString *urlString = [NSString stringWithFormat:@"%@?client_id=%@", publicTrackUrlString, yourSCClientID];
[SCRequest performMethod: SCRequestMethodGET 
              onResource: [NSURL URLWithString:urlString]
         usingParameters: nil 
             withAccount: nil
  sendingProgressHandler: nil 
         responseHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                      // 
         }];

Then it will work for public tracks when the withAccount: parameter is nil.

More elegantly, you can as well pack the "client_id" in a dictionary and use it with usingParameters:

NSDictionary *params = @{@"client_id": yourSCClientID} ;



来源:https://stackoverflow.com/questions/15537080/playing-a-soundcloud-file-in-ios-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!