ios – 不从stream_url SoundCloud API播放声音

我可以在表格视图中显示歌曲,但我无法播放.

点击获取曲目按钮时,这是我的代码

- (IBAction) getTracks:(id) sender
{

    SCRequestResponseHandler handler;
    handler = ^(NSURLResponse *response,NSData *data,NSError *error) {
        NSError *jsonError = nil;
        NSJSONSerialization *jsonResponse = [NSJSONSerialization
                                             JSONObjectWithData:data
                                             options:0
                                             error:&jsonError];
        if (!jsonError && [jsonResponse isKindOfClass:[NSArray class]]) {
            SCTTrackListViewController *trackListVC;
            trackListVC = [[SCTTrackListViewController alloc]
                           initWithNibName:@"SCTTrackListViewController"
                           bundle:nil];
            trackListVC.tracks = (NSArray *)jsonResponse;
            NSLog(@"json %@",(NSArray *)jsonResponse);
            [self presentViewController:trackListVC
                               animated:YES completion:nil];
        }
    };



    NSString *resourceURL = @"https://api.soundcloud.com/users/54237635/tracks.json?client_id=8a58d84d48322f5661cf14a3d80e005d";


    [SCRequest performMethod:SCRequestMethodGET
                  onResource:[NSURL URLWithString:resourceURL]
             usingParameters:nil
                 withAccount:nil
      sendingProgressHandler:nil
             responseHandler:handler];
}

这是我在tableview中的didselect中的代码:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSDictionary *track = [self.tracks objectAtIndex:indexPath.row];
    NSString *streamURL = [track objectForKey:@"stream_url"];


    [SCRequest performMethod:SCRequestMethodGET
                  onResource:[NSURL URLWithString:streamURL]
             usingParameters:nil
                 withAccount:nil
      sendingProgressHandler:nil
             responseHandler:^(NSURLResponse *response,NSError *error) {
                 NSError *playerError;
                 player = [[AVAudioPlayer alloc] initWithData:data error:&playerError];
                 [player prepareToPlay];
                 [player play];
             }];

    NSLog(@"streamURL %@",[track objectForKey:@"stream_url"]);
}

解决方法

由OP解决:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)
{

NSDictionary *track = [self.tracks objectAtIndex:indexPath.row];
    NSString *streamURL = [track objectForKey:@"stream_url"];

NSString *urlString = [NSString stringWithFormat:@"%@?client_id=%@",streamURL,CLIENT_ID];//Your client ID


[SCRequest performMethod:SCRequestMethodGET
              onResource:[NSURL URLWithString:urlString]
         usingParameters:nil
             withAccount:nil
  sendingProgressHandler:nil
         responseHandler:^(NSURLResponse *response,NSError *error) {
             NSError *playerError;

             NSLog(@"data:%@",data);
             player = [[AVAudioPlayer alloc] initWithData:data error:&playerError];
             [player prepareToPlay];
             [player play];
         }];
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...