So basically i want to create a music player where all users will be able to play songs which they have in their device with my own customised music player. This code I'm using right now to get the list :

import UIKit
import MediaPlayer

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let mediaItems = MPMediaQuery.songsQuery().items

    let mediaCollection = MPMediaItemCollection(items: mediaItems!)

    print(mediaItems)
  }
}

This prints a list like this :

([<MPConcreteMediaItem: 0x14de96260> 1273733459653912039, <MPConcreteMediaItem: 0x14de964f0> 3069043783347985482, <MPConcreteMediaItem: 0x14de96780> 3069043783347985483, <MPConcreteMediaItem: 0x14de96a10> 914421989453240435])

I want to show songs names and picture art in tableview, but m new with MPMediaItems.. Any help would be appreciable. Thanks

1

There are 1 best solutions below

0
AMGuru On

You can get the picture of MPMediaItem by accessing its property: artwork, and song name by property: title. The following code is in Objective-c not Swift, but it will give you some ideas.

for ( MPMediaItem *item in [MPMediaQuery songsQuery] ){
  // print name
  NSLog(@"song name: %@" , item.title);
  // set picture of the item into a UIImage
  UIImage *image = item.artwork;
  // show the image somewhere
  ...
}