0

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

Akshay
  • 11
  • 3
  • What's your question/issue? – rmaddy Sep 09 '16 at 05:29
  • @rmaddy I have this array of `mediaItems` I want to show users the songs they have in their phone with names and picture art.. like iTunes does. – Akshay Sep 09 '16 at 05:31
  • That's not a question. Don't just post what you want to do. Explain what your question or issue is. This site is for helping you fix a problem with something you've already tried. It is not for writing your code for you. – rmaddy Sep 09 '16 at 05:36
  • It would have efficient enough if you could just help answering to a beginner :) – Akshay Sep 09 '16 at 05:45
  • Possible duplicate of [this](http://stackoverflow.com/a/29139262/4933540). or [this one](http://stackoverflow.com/a/26127979/4933540). Please research first for similar questions on SO. – Yash Tamakuwala Sep 09 '16 at 06:22
  • Hey thanks Yash for your help.. I came across this link.. The problem is this plays songs in iTunes only (systemPlayer). I want play them in my customised music player – Akshay Sep 09 '16 at 06:31
  • Sorry, Akshay, I am all for helping beginners, but it would *not* be efficient to "just help answering to a beginner". You're expecting us to explain you how to write a specific app. That is not a question in the sense of SO. There's various ways you could go about your concept and what we do here is helping people with the problems they encounter on the way to implementing their chosen concept. Asking us to do the heavy lifting might be "efficient" for you, but not for us. You're asking for free labor, basically. – Gero Sep 09 '16 at 07:29
  • Possible duplicate of [iOS + Swift ,How to access all music file in directory](http://stackoverflow.com/questions/29138285/ios-swift-how-to-access-all-music-file-in-directory) – gurmandeep Sep 09 '16 at 09:03

1 Answers1

1

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
  ...
}
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
AMGuru
  • 106
  • 1
  • 3