繁体   English   中英

Soundcloud iOS API - 从链接播放声音

[英]Soundcloud iOS API - playing sound from link

我想使用Soundcloud API在我的应用程序中播放公共声音。 在网上进行了一些搜索之后,我找到了一种方法,但我不能让我的代码工作。 这是我尝试播放声音的代码:

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

您可以从解析资源中获取曲目ID: http//api.soundcloud.com/resolve.json?url = http://soundcloud.com/miroslav-osipovic/the-cat-empire-the-lost-song&client_id = YOUR_CLIENT_ID

暂无
暂无

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

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