28
連続再生アプリをつくろう Inside of Attacca Takao Funami Media Technology Labs, Recruit co ltd Twitter: @iRSS FaceBook: takao.funami AVQueuePlayer,AVSessionのTips 12721日土曜日

連続再生アプリをつくろう Inside of attacca

Embed Size (px)

DESCRIPTION

http://atnd.org/events/30549 第3回iphone_dev_jp 東京iPhone/Mac勉強会 のプレゼン資料です。

Citation preview

Page 1: 連続再生アプリをつくろう  Inside of attacca

連続再生アプリをつくろうInside of Attacca

 Takao FunamiMedia Technology Labs, Recruit co ltd

Twitter: @iRSSFaceBook: takao.funami

AVQueuePlayer,AVSessionのTips

12年7月21日土曜日

Page 2: 連続再生アプリをつくろう  Inside of attacca

自己紹介をすこし

• フナミタカオ• リクルート メディアテクノロジーラボ所属• リクルートWebサービス開発担当 2007~• iOS 2008~• Android 2010年頃すこし

12年7月21日土曜日

Page 3: 連続再生アプリをつくろう  Inside of attacca

iPhone

2009年頃の初期バージョンを開発http://mtl.recruit.co.jp/blog/2009/09/926foomoo_by_for_iphone.html

12年7月21日土曜日

Page 4: 連続再生アプリをつくろう  Inside of attacca

iPad

2010年5月 じゃらん沖縄iPadhttp://mtl.recruit.co.jp/blog/2010/09/ipad.html

12年7月21日土曜日

Page 5: 連続再生アプリをつくろう  Inside of attacca

Android

http://mtl.recruit.co.jp/blog/2010/01/android.html2010年1月 ホットペッパー for Adroid

12年7月21日土曜日

Page 6: 連続再生アプリをつくろう  Inside of attacca

2011

http://itun.es/i6jS7pFhttp://itun.es/i6jY7dZ12年7月21日土曜日

Page 7: 連続再生アプリをつくろう  Inside of attacca

2012 - Attaccaの音楽再生モジュールを開発しました

Share

PlayList

Search

Streaming Music

Ranking

Connection

12年7月21日土曜日

Page 8: 連続再生アプリをつくろう  Inside of attacca

ところで、Attaccaインストールされましたよね?

•無料ミュージック2位• iTunesStore試聴連続再生•広告なし• JP,US,UKランキング対応•豊富なテーマ別プレイリスト•年代別Hits、フジロックetc

12年7月21日土曜日

Page 9: 連続再生アプリをつくろう  Inside of attacca

ところで、Attaccaインストールされましたよね?

•無料ミュージック2位• iTunesStore試聴連続再生•広告なし• JP,US,UKランキング対応•豊富なテーマ別プレイリスト•年代別Hits、フジロックetc

本日:無料ミュージック28位 -まだのかた今すぐダウンロードお願いします!

12年7月21日土曜日

Page 10: 連続再生アプリをつくろう  Inside of attacca

7/20 アップデート

12年7月21日土曜日

Page 11: 連続再生アプリをつくろう  Inside of attacca

7/20 アップデート

•シャッフル機能追加• Twitter Framework対応

12年7月21日土曜日

Page 12: 連続再生アプリをつくろう  Inside of attacca

本題

Attaccaの音楽再生モジュール開発の知見共有します

12年7月21日土曜日

Page 13: 連続再生アプリをつくろう  Inside of attacca

iTunes試聴音楽連続再生アプリの基本要件

12年7月21日土曜日

Page 14: 連続再生アプリをつくろう  Inside of attacca

iTunes試聴音楽連続再生アプリの基本要件

• 取得したリストから再生• 再生、停止• バックグラウンドでも再生• コントローラーに対応• ロック画面のコントローラーに対応

• 電話等の割り込み後に復帰

12年7月21日土曜日

Page 15: 連続再生アプリをつくろう  Inside of attacca

音楽のリストを取得して、再生するしくみ

iTunes Search API

iTunes Store RSShttp://itunes.apple.com/jp/rss/itunes affiliates resources Search APIhttp://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html

Music

JSON取得

iTunes Store RSS

Music Musicurl,nameimage..

Parser

AVQueuePlayer

AVPlayerItem AVPlayerItem AVPlayerItem

DataSorce

Musicクラスの配列

Music再生Manager

コントロールに反応

インスタンス化

12年7月21日土曜日

Page 16: 連続再生アプリをつくろう  Inside of attacca

サーバーサイド(おまけ)

http://i2key.hateblo.jp/entry/playstudy3参照:第三回Playframework勉強会で発表してきました。

12年7月21日土曜日

Page 18: 連続再生アプリをつくろう  Inside of attacca

AVPlayerItemの配列からAVQueuePlayerを作る

• // _palyURLs は再生したいURLの配列

• NSMutableArray *palyerItems = [NSMutableArray array];

int musicCount = [_playList count]; // palyerItems に playerItemを追加しまくる for (int i = index ; i < musicCount ; i++){ NSURL *url = [_palyURLs objectAtIndex:i]; if (url != nil){ AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:url]; [palyerItems addObject:playerItem]; } } // AVQueuePlayerのインスタンスつくる AVQueuePlayer *player = [AVQueuePlayer queuePlayerWithItems:palyerItems]; [player play];

•-ストリーミング再生 - 3Gでも再生開始早い-先読み - 面倒なキャッシュを勝手にやってくれる うれしい※ iTunesアフィリの規約で、ダウンロード再生は禁止

簡単!

12年7月21日土曜日

Page 19: 連続再生アプリをつくろう  Inside of attacca

AVQueuePlayerのコントロール

• AVPlayer• 再生 - (void)play;• 停止 - (void)pause;• 再生中のアイテム @property (nonatomic, readonly) AVPlayerItem *currentItem;

• AVQueuePlayer• 次の曲再生 - (void)advanceToNextItem;

弱点:戻るがない! & Fade IN/OUTできない

12年7月21日土曜日

Page 20: 連続再生アプリをつくろう  Inside of attacca

AVQueuePlayer(AVPlayer)のステイタス監視

[self.player addObserver:self forKeyPath:@"status" options:0 context:&PlayerStatusContext];

[self.player addObserver:self forKeyPath:@"currentItem" options:0 context:&CurrentItemChangedContext];

[self.player addObserver:self forKeyPath:@"rate" options:0 context:&PlayerRateContext];

↑だけではたりない

AVPlayerItem *playerItem = [self.player currentItem];

playerItem.playbackLikelyToKeepUp

AVPlayerItemのステイタスもみる必要あり

もれがたくさんありそうなので、調査中です弱点:通信状態のエラーハンドリング不安

12年7月21日土曜日

Page 21: 連続再生アプリをつくろう  Inside of attacca

2.バックグラウンドでも再生

• info.plist UIBackgroundModesをaudioに

<key>UIBackgroundModes</key> <array> <string>audio</string> </array>

• AVAudioSessionのカテゴリをAVAudioSessionCategoryPlaybackに

12年7月21日土曜日

Page 22: 連続再生アプリをつくろう  Inside of attacca

AVQueuePlayer勉強のための必見のビデオ

• 405_exploring_av_foundation 2010 WWDCビデオ

12年7月21日土曜日

Page 23: 連続再生アプリをつくろう  Inside of attacca

3.電話とかの割り込み後復帰

• AVAudioSessionDelegateに対応すればOK#pragma mark -#pragma mark Interruption event handling- (void)beginInterruption{ if (self.playingMusic){ self.shouldResume = YES; }else{ self.shouldResume = NO; } self.playingMusic = NO;}

- (void)endInterruptionWithFlags:(NSUInteger)flags{ if (flags == AVAudioSessionInterruptionFlags_ShouldResume){ [[AVAudioSession sharedInstance] setActive: YES error: nil]; if (self.shouldResume){ [self play]; } } }

biginで状態保持して、endで状態におおじて必要な場合は再生再開

12年7月21日土曜日

Page 24: 連続再生アプリをつくろう  Inside of attacca

4.リモートコントローラ対応-­‐  (void)  viewDidAppear:(BOOL)animated  {        if  ([[UIApplication  sharedApplication]  respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){                [[UIApplication  sharedApplication]  beginReceivingRemoteControlEvents];                [self  becomeFirstResponder];        }}

-­‐  (void)  viewWillDisappear:(BOOL)animated  {        [[UIApplication  sharedApplication]  endReceivingRemoteControlEvents];        [self  resignFirstResponder];}

#pragma mark -#pragma mark Remote-control event handling// Respond to remote control events- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent { if (receivedEvent.type == UIEventTypeRemoteControl) { switch (receivedEvent.subtype) { case UIEventSubtypeRemoteControlTogglePlayPause: [self playOrPause]; break; case UIEventSubtypeRemoteControlPreviousTrack: [self prev]; break; case UIEventSubtypeRemoteControlNextTrack: [self next]; break; default: break; } }}

12年7月21日土曜日

Page 25: 連続再生アプリをつくろう  Inside of attacca

5.ロック画面へのアートワーク表示- (NSDictionary *)songInfo{ NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init]; [songInfo setObject:self.title forKey:MPMediaItemPropertyTitle]; [songInfo setObject:self.author forKey:MPMediaItemPropertyArtist]; [songInfo setObject:self.album forKey:MPMediaItemPropertyAlbumTitle]; if (self.artwork){ [songInfo setObject:self.artwork forKey:MPMediaItemPropertyArtwork]; }else{ [songInfo setObject:[[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"glay.png"]] forKey:MPMediaItemPropertyArtwork]; } /* コピーしたほうが、安全かもと思い、コピー */ return [NSDictionary dictionaryWithDictionary:songInfo];}

- (void)updatePlayingInfo{ Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter"); if (playingInfoCenter) { NSDictionary *songInfo = [self.currentMusic songInfo]; [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo]; //NSLog(@"songInfo:%@",songInfo); }}

MPNowPlayingInfoCenterを使えばOK ただし iOS5.0以上

12年7月21日土曜日

Page 26: 連続再生アプリをつくろう  Inside of attacca

これらのサンプルソース作成中です

• あとで gitあげます

12年7月21日土曜日

Page 27: 連続再生アプリをつくろう  Inside of attacca

今後

• Fade IN / OUTしたいとおもうので、根本的に見直す必要があるかもとおもっています。

• でも、とりあえず、AVQueuePlayer快適なので、簡単アプリ作成にはGood

• iTunesの規約もあって、オフライン対応していませんが、自分用には、オフライン対応して、朝の電車でも快適連続再生

12年7月21日土曜日

Page 28: 連続再生アプリをつくろう  Inside of attacca

最後に

• AVQueuePlayer快適なのでAttaccaよろしくおねがいします

12年7月21日土曜日