我有一个用SoundCloud制作播放列表的网站。现在我想为iPhone制作一个应用程序,这样用户也可以在那里收听歌曲。
示例中的链接中的http://developers.soundcloud.com/docs/api/ios-quickstart用户必须登录才能收听和分享,但我希望我的用户只收听。有没有办法让他们不用登录?
发布于 2013-04-12 04:24:28
创建一个以JSON格式输出播放列表的页面,然后在xcode中创建一个类,用于下载曲目字典的JSON,并使用AVPlayer播放下载的内容(如果您正在播放整个列表,则使用AVQueuePlayer )。
下面是一些抽象代码:
playlistDownloader.m
- (void)downloadPlaylist{
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(concurrentQueue, ^{
NSURL *url = [NSURL URLWithString:@"http://www.yourwebsite.com/playlist.json?id=1"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
id trackData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (!error) {
tempTrackArray = trackData;
} else {
NSLog(@"Playlist wasn't able to download");
}
});
}tempTrackArray将是在类中声明的属性。
然后在你的播放器中,你会这样做:
audioPlayer.m
- (void)instanciateAudioPlayer
{
NSDictionary *trackDictionary = [playListDownloader.tempTrackArray objectAtIndex:0];
NSString *urlString = [trackDictionary objectForKey:@"stream_url"];
AVAsset *asset = [AVAsset assetWithURL:streamURL];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[avPlayer initWithPlayerItem:playerItem];
}这是一些非常粗糙的代码,但它是你想要做的事情的大致要点。应该会让你走上正确的方向。
发布于 2013-12-03 15:02:17
尝试soundcloud quickstart,我意识到你的流URL需要以https而不是http开头。此外,您还需要将您的客户端id从您的声音云应用程序添加到流url:
NSDictionary *trackDictionary = [playListDownloader.tempTrackArray objectAtIndex:0];
NSString *streamURL = [trackDictionary objectForKey:@"stream_url"];
streamURL = [streamURL stringByReplacingOccurrencesOfString:@"http" withString:@"https"];
NSString *urlString = [NSString stringWithFormat:@"%@?client_id=%@", streamURL, @"a8e117d3fa2121067e0b29105b0543ef"];在那里,您只需设置AVPlayer:
self._avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlString]];
[self setupLayer:clayer];
[self._avPlayer play];一切都应该正常工作。希望能有所帮助
https://stackoverflow.com/questions/13111391
复制相似问题