Soundcloud 应用程序如何快速跳到下一首曲目?
How does Soundcloud app skip to next track so fast?
我正在构建我的第一个应用程序,它是一个 Soundcloud 客户端。现在,每次选择要播放的曲目时,我都必须继续执行 NSURLSessionDataTask 提取以获取二进制数据,但这需要很长时间。当我跳到下一首曲目时,我必须添加逻辑以在当前歌曲播放时提前下载下一首歌曲……不过,如果用户快速跳过,这仍然很慢:/
Soundcloud 本机应用程序会立即跳过。它是如何工作的?我试过使用 Soundcloud iOS SDK,但现在已弃用。
这是我的歌曲提取:
-(void)fetchTrack: (SCTrack*)selectedTrack completionHandler: (void(^)(NSData *trackData, NSString *error)) completionHandler; {
NSString* clientID = @"41a5278fd8c704c3eb5a4a0ca38f9036";
NSString* streamURL = selectedTrack.stream_url;
NSString* urlString = [NSString stringWithFormat:@"%@?client_id=%@", streamURL, clientID];
NSURL* url = [[NSURL alloc]initWithString:urlString];
NSLog(@"%@", urlString);
NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:url];
request.HTTPMethod = @"GET";
NSURLSessionDataTask* dataTask = [[self session] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse* callResponse = (NSHTTPURLResponse*)response;
if ([callResponse isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger responseCode = [callResponse statusCode];
if (responseCode >= 200 && responseCode <= 299) {
NSData* trackData = data;
NSLog(@"STREAM 200");
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
NSLog(@"%@", data);
completionHandler(trackData, nil);
}];
}else{
NSLog(@"%ld", (long)responseCode);
}
}
}];
[dataTask resume];
}
这就是我正在播放并尝试在播放当前曲目时获取下一首歌曲的方式:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedTrack = self.SCTrackList[indexPath.row];
self.selectedTrackRow = indexPath.row;
[[SoundCloudAPI sharedInstance]fetchTrack:self.selectedTrack completionHandler:^(NSData *trackData, NSString *error) {
self.player = [[AVAudioPlayer alloc]initWithData: trackData error:nil];
[self.player prepareToPlay];
[self.player play];;
}];
[self prepareForNextTrack:self.selectedTrack];
}
-(void)prepareForNextTrack: (SCTrack*)trackPlaying {
self.selectedTrackRow += 1;
self.selectedTrack = self.SCTrackList[self.selectedTrackRow];
[[SoundCloudAPI sharedInstance]fetchTrack:self.selectedTrack completionHandler:^(NSData *trackData, NSString *error) {
self.trackDataToPlay = trackData;
}];
}
- (IBAction)nextPressed:(id)sender {
self.player = [[AVAudioPlayer alloc]initWithData: self.trackDataToPlay error:nil];
[self.player prepareToPlay];
[self.player play];;
[self prepareForNextTrack:self.selectedTrack];
}
此外,我是新手,所以我确信我的代码很笨拙,如果有人能指出改进方法,我将不胜感激。
谢谢指点!
原来我把事情搞得比原来更复杂了。我所要做的就是使用 dataWithContentsOfURL 方法来检索传入令牌的歌曲。
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?oauth_token=%@",self.selectedTrack.stream_url, self.token]]];
当然,把数据放到AVAudioPlayer里面。
基本上,去掉上面的 fetchTrack 函数。
我正在构建我的第一个应用程序,它是一个 Soundcloud 客户端。现在,每次选择要播放的曲目时,我都必须继续执行 NSURLSessionDataTask 提取以获取二进制数据,但这需要很长时间。当我跳到下一首曲目时,我必须添加逻辑以在当前歌曲播放时提前下载下一首歌曲……不过,如果用户快速跳过,这仍然很慢:/
Soundcloud 本机应用程序会立即跳过。它是如何工作的?我试过使用 Soundcloud iOS SDK,但现在已弃用。
这是我的歌曲提取:
-(void)fetchTrack: (SCTrack*)selectedTrack completionHandler: (void(^)(NSData *trackData, NSString *error)) completionHandler; {
NSString* clientID = @"41a5278fd8c704c3eb5a4a0ca38f9036";
NSString* streamURL = selectedTrack.stream_url;
NSString* urlString = [NSString stringWithFormat:@"%@?client_id=%@", streamURL, clientID];
NSURL* url = [[NSURL alloc]initWithString:urlString];
NSLog(@"%@", urlString);
NSMutableURLRequest* request = [[NSMutableURLRequest alloc]initWithURL:url];
request.HTTPMethod = @"GET";
NSURLSessionDataTask* dataTask = [[self session] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSHTTPURLResponse* callResponse = (NSHTTPURLResponse*)response;
if ([callResponse isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger responseCode = [callResponse statusCode];
if (responseCode >= 200 && responseCode <= 299) {
NSData* trackData = data;
NSLog(@"STREAM 200");
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
NSLog(@"%@", data);
completionHandler(trackData, nil);
}];
}else{
NSLog(@"%ld", (long)responseCode);
}
}
}];
[dataTask resume];
}
这就是我正在播放并尝试在播放当前曲目时获取下一首歌曲的方式:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.selectedTrack = self.SCTrackList[indexPath.row];
self.selectedTrackRow = indexPath.row;
[[SoundCloudAPI sharedInstance]fetchTrack:self.selectedTrack completionHandler:^(NSData *trackData, NSString *error) {
self.player = [[AVAudioPlayer alloc]initWithData: trackData error:nil];
[self.player prepareToPlay];
[self.player play];;
}];
[self prepareForNextTrack:self.selectedTrack];
}
-(void)prepareForNextTrack: (SCTrack*)trackPlaying {
self.selectedTrackRow += 1;
self.selectedTrack = self.SCTrackList[self.selectedTrackRow];
[[SoundCloudAPI sharedInstance]fetchTrack:self.selectedTrack completionHandler:^(NSData *trackData, NSString *error) {
self.trackDataToPlay = trackData;
}];
}
- (IBAction)nextPressed:(id)sender {
self.player = [[AVAudioPlayer alloc]initWithData: self.trackDataToPlay error:nil];
[self.player prepareToPlay];
[self.player play];;
[self prepareForNextTrack:self.selectedTrack];
}
此外,我是新手,所以我确信我的代码很笨拙,如果有人能指出改进方法,我将不胜感激。
谢谢指点!
原来我把事情搞得比原来更复杂了。我所要做的就是使用 dataWithContentsOfURL 方法来检索传入令牌的歌曲。
NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?oauth_token=%@",self.selectedTrack.stream_url, self.token]]];
当然,把数据放到AVAudioPlayer里面。
基本上,去掉上面的 fetchTrack 函数。