LoginSignup
9

More than 5 years have passed since last update.

SoundCloud iOSアプリ開発 : プレイリスト取得

Last updated at Posted at 2014-07-31

SoundCloudのAPIをつかったiOSアプリ開発の手順/ノウハウをまとめていくシリーズ。

SoundCloudのプレイリストからトラックの一覧を取得したいと思います。

はじめに

英語がスラスラ読める人は以下のSoundCloudのクイックスタートマニュアルも合わせて参照した方が理解が深まります。

参考:SoundCloud iOS SDK
http://developers.soundcloud.com/docs/api/ios-quickstart

プレイリスト取得/表示

事前準備

事前にアカウント認証を済ませておく必要があります。
アカウント認証方法は、
SoundCloud iOSアプリ開発 : アカウント認証
もしくは、
SoundCloudクイックスタートマニュアル
をご参考ください。

プレイリスト取得

getTracksButtonを押したらgetTracksメソッドを実行し
任意のプレイリストを取得する機能を実装します。

ボタンのプロパティを宣言します。

@property (weak, nonatomic) IBOutlet UIButton *getTracksButton;
- (IBAction)getTracks:(id)sender;

getTracksButtonボタンを押したら、実行されるgetTracksメソッドを記述します。

- (IBAction)getTracks:(id)sender {
    SCAccount *account = [SCSoundCloud account];
    if (account == nil) {
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Not Logged In"
                              message:@"You must login first"
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
        [alert show];
        return;
    }

    SCRequestResponseHandler handler;
    handler = ^(NSURLResponse *response, NSData *data, NSError *error) {
        NSError *jsonError = nil;
        NSDictionary *jsonResponse = [NSJSONSerialization
                                             JSONObjectWithData:data
                                             options:1
                                             error:&jsonError];

        NSArray *array = nil;
        array = [jsonResponse objectForKey:@"tracks"];
        if (!jsonError && array) {
            // xibの場合の記述はこちら                       
            SCTTrackListViewController *trackListVC = [[self storyboard] instantiateViewControllerWithIdentifier:@"SCTTrackListViewController"];

            trackListVC.tracks = (NSArray *)array;
            [self presentViewController:trackListVC animated:YES completion:nil];
        }
    };

    // playlist取得のリクエストを送る
   [SCRequest performMethod:SCRequestMethodGET
                  onResource:[NSURL URLWithString:@"https://api.soundcloud.com/playlists/xxxxxxxx.json"]
             usingParameters:nil
                 withAccount:account
      sendingProgressHandler:nil
             responseHandler:handler];
}

SCRequestのperformMethodメソッドのURLWithStringの引数に指定するURLですが、
"xxxxxxxx"となっているところはプレイリストのIDを記載してください。

プレイリストのIDは、ブラウザからSoundCloudのプレイリストを開いて、
Shareボタン -> Embed -> でinframeタグの中に記載されているurlの..../playlist/xxxxxxxx&...と記載されている"xxxxxxxx"のところです。

例えば、僕のプレイリストならば
https://soundcloud.com/ryotaro-ohkawa/sets/mybesttracks-2013

Embed用のinframeタグに記載されているIDは18368246となりますので、そのIDを上述の"xxxxxxxx"に記載します。

<iframe width="100%" height="450" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/playlists/18368246&amp;auto_play=false&amp;hide_related=false&amp;visual=true"></iframe>

プレイリストをリスト表示

プレイリストを取得できたら、書くまでもないかもしれませんが、UITableViewクラスを使ってリスト表示をします。

テーブル全体のセクションの数を返す

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

指定されたセクションの項目数を返す

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.tracks count];
}

指定された箇所のセルを作成する。

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:CellIdentifier];
    }

    NSDictionary *track = [self.tracks objectAtIndex:indexPath.row];
    cell.textLabel.text = [track objectForKey:@"title"];

    return cell;
}

プレイリストのトラックの一覧を格納するtracks はプロパティ変数として定義しておいてください。
これで、getTracksButtonを押したらgetTracksメソッドを実行し僕のプレイリストからトラックの一覧を取得できました。

soundcloud_getPlaylist_demo.png

参考

Soundcloud iOS SDK ガイドライン
http://developers.soundcloud.com/docs/api/ios-quickstart#installation

Soundcloud HTTP API Reference
https://developers.soundcloud.com/docs/api/reference#playlists

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9