iPod Library Access
So you want your app to somehow process raw PCM samples from the user’s iPod Library in iOS4. Maybe you need to timestretch those tracks for a DJ app, or maybe you want to write a visualizer. Or maybe you want to write an app that makes it sound like your user’s entire music library was performed by Darth Vader.
In versions of iOS prior to 4.0, you couldn’t get access to those bits. You could use the MediaPlayer framework to browse and play files from the user’s iPod Library, but you couldn’t actually process those files.
I’m happy to report that thanks to additions to the AVFoundation and MediaPlayer frameworks in iOS 4.0, you can create local copies of iPod Library files for your app to open. Basically, you use a new property on MPMediaItem—MPMediaItemPropertyAssetURL—to get a special URL that can only be used with AVFoundation apis. You then use this URL to create an AVAssetExportSession and ‘export’ the file to somewhere in your app’s local directory structure.
What’s somewhat tricky is choosing values for the preset and outputFileType properties of AVAssetExportSession. Without the exact right combination of input file type, preset and outputFileType, you’ll get cryptic errors, zero-length output files, or ridiculously long transcoding times.
Enter TSLibraryImport, a simple, MIT-licensed open-source class that makes it trivial (one method call) to get iPod Library files copied into your app for further processing. You can grab the source over at the project’s bitbucket
I hope you find it useful, and if you make something cool with it, please let me know!
UPDATE 7/14/10: If you have an iPhone Developer Account, you may want to have a look at today’s 4.1 SDK beta before using TSLibraryImport.
UPDATE 7/19/10: Chris Adamson has an excellent write-up on getting files out of the iPod Library over at Time Code Our methods are similar—he transcodes files to m4a/aac on import while TSLibraryImport uses passthrough—but his explanation of how this all works vis-á-vis AVAssetExportSession is lucid, detailed and well-written.