iOS Components and Frameworks: Understanding the Advanced Features of the iOS SDK
by Kyle Richter, Joe Keeley
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access