SoundCloud API iOS, no sharing - listening only

I have a website where I create playlists with SoundCloud. Now I want to make an iPhone app so that users can listen to songs there.

http://developers.soundcloud.com/docs/api/ios-quickstart In the example in the link, users must log in to listen and share, but I want my users to only listen. Is there any way that they cannot enter?

+4
source share
2 answers

Create a page that displays the playlist in JSON, and then in xcode create a class that loads JSON for track dictionaries and plays the downloaded content using AVPlayer (or AVQueuePlayer if you play the entire list).

Here is some abstract code:

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 will be the declaration declared in the class.

Then in your player you will do something like this:

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

This is some really crude code, but its general meaning is what you are trying to do. Should make you go in the right direction.

+1
source

Having tried the quick start of soundcloud, I realized that your stream should start with https, not http. You also need to add your client ID from the sound cloud application to the stream 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"]; 

From there, you simply install AVPlayer:

 self._avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:urlString]]; [self setupLayer:clayer]; [self._avPlayer play]; 

Everything should work fine. Hope this helps.

+1
source

Source: https://habr.com/ru/post/1442624/


All Articles