0
votes

I have the reference of a MPMediaItem when user selects an audio from the iPod library. I am getting the asset url of that item by using

let url = item.valueForProperty(MPMediaItemPropertyAssetURL) But this is not giving me the exact physical location of the file, instead it is giving me an url w.r.t iPod library.

ipod-library://item/item.mp3?id=1840064795502796074

Is there a way to get the physical url of a song from iPod library?

EDIT - actually I want to extract NSData from the physical file and send it to my backend server, so i need the physical file URL and not the relative URL

1
You can't do that. The file is outside your app's sandbox so you can't access it directly. You can only ask the MPMediaPlayer to play it for you.Paulw11
HI ibrahim, I tried this already. i need the physical file URL and not the relative URL.user7142042
Hi pailw, I saw the same kind of my app. I downloaded the app.It's working good.If i click the song and done button...The song will be displayed in our app and played.I can trim and merge the song also in that app.. This is the link:itunes.apple.com/us/app/handy-audio-editor-trim-audio/…user7142042

1 Answers

1
votes

Music files are represented by MPMediaItem instances. You could use retrieve them through a MPMediaQuery, for example :

// All
let mediaItems = MPMediaQuery.songsQuery().items
// Or you can filter on various property
// Like the Genre for example here
var query = MPMediaQuery.songsQuery()
let predicateByGenre = MPMediaPropertyPredicate(value: "Rock", forProperty: MPMediaItemPropertyGenre)
query.filterPredicates = NSSet(object: predicateByGenre)

At this point, you have all (or some if you filter) songs included in Music App Library, so you can play them with a MPMusicPlayerController after setting a playlist queue :

let mediaCollection = MPMediaItemCollection(items: mediaItems)

let player = MPMusicPlayerController.systemMusicPlayer()
player.setQueueWithItemCollection(mediaCollection)

player.play()

There is probably the possibility to access somehow the metadata (title, genre, artist, ...) from the songs.

This probably won't work on simulator.