iOS デバイスで現在 iPod で再生中の曲情報を取得するには
iPhone, iPad, iPod touch で音楽などを再生したりするには、MediaPlayer.framework を使います。
MPMusicPlayerController が音楽を再生を制御するコントローラクラスでこのクラスメソッドの iPodMusicPlayer を使うと iPod のコントローラが取得できます。
さらに nowPlayingItem プロパティからその名の通り再生中(一時停止している場合も含む)のデータを MPMediaItem インスタンスで取得できます。
MPMediaItem からメタデータを各フィールドを表す定数を指定して取り出せます。アイテムの種別は MPMediaItemPropertyMediaType で NSInteger をカプセル化した NSNumber で取得できます。
以下は、曲を再生中の場合に曲名、アルバム名、アーティストそしてアートワークの80x80画像をそれぞれ取得しています。
#import <MediaPlayer/MediaPlayer.h> MPMusicPlayerController *pc = [MPMusicPlayerController iPodMusicPlayer]; MPMediaItem *playingItem = [pc nowPlayingItem]; if (playingItem) { NSInteger mediaType = [[playingItem valueForProperty:MPMediaItemPropertyMediaType] integerValue]; if (mediaType == MPMediaTypeMusic) { NSString *songTitle = [playingItem valueForProperty:MPMediaItemPropertyTitle]; NSString *albumTitle = [playingItem valueForProperty:MPMediaItemPropertyAlbumTitle]; NSString *artist = [playingItem valueForProperty:MPMediaItemPropertyArtist]; textView.text = [NSString stringWithFormat:@"%@ - %@ / %@", artist, songTitle, albumTitle]; MPMediaItemArtwork *artwork = [playingItem valueForProperty:MPMediaItemPropertyArtwork]; UIImage *artworkImage = [artwork imageWithSize:CGSizeMake(80.0, 80.0)]; UIImageView *artworkImageView = [[UIImageView alloc] initWithImage:artworkImage]; } }