Playing a Random Song

Without providing any predicate parameters, the MPMediaQuery will contain all the items found within the music library. A new NSArray is created to hold onto these items, which are retrieved using the item’s method on the MPMediaQuery. Each item is represented by an MPMediaItem. The random song functionality of the sample app will play a single song at a time. If no songs were found in the query a UIAlert is presented to the user; however if multiple songs are found a single one is randomly selected.

After a single (or multiple) MPMediaItem has been found, a new MPMediaItemCollection is created by passing an array of MPMediaItems into it. This collection will serve as a playlist for the MPMusicPlayerController. After the ...

Get iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.