The jonki

呼ばれて飛び出てじょじょじょじょーんき

【iOS】iPod Library Accessの使い方(3)バックグラウンドでの再生に対応する

前回の続きです。
【iOS】iPod Library Accessの使い方(2)簡易音楽プレイヤーを作る


結論から入るとMPMusicPlayerControllerにはapplicationMusicPlayerではなく、iPodMusicPlayerをセットすることでバックグラウンド再生に対応しました。対応したというよりは通常のiPodと同じコントローラーを使っているので、標準のiPodアプリと同じ挙動をするようになりました。

@property MPMusicPlayerController *player;

//    _player = [MPMusicPlayerController applicationMusicPlayer];
    _player = [MPMusicPlayerController iPodMusicPlayer];

applicationMusicPlayerとiPodMusicPlayerの違いについて

ドキュメントにはこう書いてある。
iPodMusicPlayer

Description
Returns the iPod music player, which controls the iPod app’s state.
The iPod music player employs the iPod app on your behalf. On instantiation, it takes on the current iPod app state and controls that state as your app runs. Specifically, the shared state includes the following:

applicationMusicPlayer

Description
Returns the application music player.
The application music player plays music locally within your app. It does not affect the iPod state. When your app moves to the background, the music player stops if it was playing.

触っているうちに分かってきました。基本的にはAudio Sourceにアクセスするのは同じですが、iPodMusicPlayerは標準のiPodアプリのインタフェースとして作られているっぽい。つまり標準のiPodの動作と連動します。たとえばiPod坂本真綾のあるアルバムを流しているとします。そこで前回作ったアプリを起動すると坂本真綾のそのアルバムが表示され、操作することができます。ただapplicationMusicPlayerだとiPodアプリとは関係ないので坂本真綾は表示されません。applicationMusicPlayerだと自分で再生キューのセットからしないといけないです。

applicationMusicPlayerだとiPodと排他的に作れるけど、基本的には音源を複数再生するってのはできないのでiPodMusicPlayerを使っていけばよさそう。単純にiPodの操作をしたいだけの人とかは特に。