Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In iOS, can I access the iTunes song list?

Tags:

ios

itunes

I'm looking for a way to programmatically (i.e., not via UI) access a users iTunes music library from my iOS app.

(Yes, I am aware that Apple discourages alternate iPod-interface apps. That's not the question :)

What I'd like to do is:

  • Gather a list of song names.
  • Have my app make an anagram of them.
  • Have the user guess the original song name.
  • If they guess right, I play a snippet of the song.

(That's not my real application, but it contains all the pieces I need.) The parts that I'm asking about are: How to get a list of song names and, given a song name, how to get the media to play it?

I think that AVAssetReader is what will let me play a song, once I have its name, so I can look that up on my own (bonus points for pointers there, though!) -- so the bottom-line question is: How to get a list of song titles from a user's iTunes Library in iOS?

like image 643
Olie Avatar asked Mar 29 '12 01:03

Olie


2 Answers

You'll want to use MPMediaQuery to get an NSArray of all the songs. Check out this link.

To play the song, I would suggest using AVPlayer.

like image 110
Letrstotheprez Avatar answered Jan 04 '23 15:01

Letrstotheprez


Check out the docs, especially the section titled "Getting Media Items Programmatically".

If the media item picker doesn’t provide the control you want, you can use the database access classes from this API. These classes are designed to let you create arbitrarily complex queries. You could, for example, retrieve all the songs in a particular genre whose titles include a particular word or phrase.

Using programmatic access is a two step process:

Configure a query. Ask the query to retrieve its matching media items. A media query is a description of what to retrieve from the device iPod library and how those retrieved items should be arranged. It has two properties to configure:

The filter is the description of what to retrieve. The filter is optional; a filterless query matches the entire iPod library. The grouping type is an optional key that specifies the arrangement to use for retrieved collections of media items. Zooming in a bit more, the filter can be as simple or complex as your application demands. It consists of one or more instances of a media property predicate. A media property predicate is a statement of a logical condition to test each media item against. The items that satisfy the filter are retrieved from the iPod library when you invoke the query.

The optional grouping type specifies the arrangement and sorting of collections as well as the sorting of media items within each collection. For example, using an “album” grouping type results in returned media items grouped by album, with each album’s songs sorted in track order.

like image 33
sosborn Avatar answered Jan 04 '23 15:01

sosborn