How to present a music library using a view controller for MPMediaPickerControllerDelegate using Swift 3 iOS10?

I am trying to follow a recent post about using MPMediaPickerControllerDelegate to represent a music selection list.

The tutorial is located at this url:

http://www.justindoan.com/tutorials/

I am using this code:

 import UIKit import MediaPlayer class ViewController: UIViewController, MPMediaPickerControllerDelegate { var mediapicker1: MPMediaPickerController! override func viewDidLoad() { super.viewDidLoad() let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music) mediaPicker.allowsPickingMultipleItems = false mediapicker1 = mediaPicker mediaPicker.delegate = self self.presentViewController(mediapicker1, animated: true, completion: nil) } } 

However, I found that:

 self.presentViewController(mediapicker1, animated: true, completion: nil) 

does not work. Unfortunately, Swift 3 suggested an automatic solution does not work:

 self.present(mediapicker1, animated: true, completion: nil) 

In addition, iOS 10 beta release notes found at:

https://www.scribd.com/doc/315770725/IOS-10-Beta-Release-Notes

says on page 10 of 18,

The MPMediaPickerController object may not display as expected.

I spent a lot of time solving this problem myself without success.

Any suggestions?

+5
source share
2 answers

Go through the steps:

  • Add "NSAppleMusicUsageDescription" to your Info.plist for the privacy authority.
  • Make sure your music app is available on your iPhone. It will not work in the simulator.
+16
source

In accordance with our discussion of comments, I recently used MPMediaPickerController in my last application called playmates . I am sharing working code with you. I wrote the code without explanation.

 import MediaPlayer class viewControllerName: UIViewController,MPMediaPickerControllerDelegate { //Below is Inaction for picking music from media library @IBAction func btnMediaPickerAction(_ sender: UIButton) { let mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.music) mediaPicker.delegate = self mediaPicker.allowsPickingMultipleItems = false self.present(mediaPicker, animated: true, completion: nil) } // MPMediaPickerController Delegate methods func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) { self.dismiss(animated: true, completion: nil) } func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) { self.dismiss(animated: true, completion: nil) print("you picked: \(mediaItemCollection)")//This is the picked media item. // If you allow picking multiple media, then mediaItemCollection.items will return array of picked media items(MPMediaItem) } } 

I found below code in your code:

  • use self.present not self.presentViewController
  • No need to create a global instance of mediaPicker . As in the delegate method, you get an instance of MPMediaPickerController
+3
source

All Articles