如何使用 Swift 3 iOS10 为 MPMediaPickerControllerDelegate 呈现带有视图控制器的音乐库?
How Do I Present a Music Library With a View Controller for a MPMediaPickerControllerDelegate using Swift 3 iOS10?
我正在尝试关注最近 post 关于使用 MPMediaPickerControllerDelegate
显示音乐选择列表的内容。
教程可在此处找到 URL:
http://www.justindoan.com/tutorials/
我正在使用此代码:
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)
}
}
但是我发现:
self.presentViewController(mediapicker1, animated: true, completion: nil)
不起作用。不幸的是,Swift 3 建议的自动解决方案也不起作用:
self.present(mediapicker1, animated: true, completion: nil)
此外,iOS 10 Beta 发行说明位于:
https://www.scribd.com/doc/315770725/IOS-10-Beta-Release-Notes
第 10 页,共 18 页,
An MPMediaPickerController object may not display as expected.
我花了很多时间想自己解决这个问题,但没有成功。
有什么建议吗?
执行以下步骤:
- 将 'NSAppleMusicUsageDescription' 添加到您的 Info.plist 隐私授权。
- 确保您的音乐应用在 iPhone 中可用。它不会在模拟器中运行。
根据我们对评论的讨论,我最近在我的最新应用 named playmates 中使用了 MPMediaPickerController
。我正在与您分享工作代码。我已经编写了不言自明的代码。
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)
}
}
我发现您的代码存在以下差异:
- 使用
self.present
而不是 self.presentViewController
- 无需创建
mediaPicker
的全局实例。与在委托方法中一样,您正在获取 MPMediaPickerController
的实例
添加隐私 - info.plist
中的媒体库使用说明
我正在尝试关注最近 post 关于使用 MPMediaPickerControllerDelegate
显示音乐选择列表的内容。
教程可在此处找到 URL:
http://www.justindoan.com/tutorials/
我正在使用此代码:
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)
}
}
但是我发现:
self.presentViewController(mediapicker1, animated: true, completion: nil)
不起作用。不幸的是,Swift 3 建议的自动解决方案也不起作用:
self.present(mediapicker1, animated: true, completion: nil)
此外,iOS 10 Beta 发行说明位于:
https://www.scribd.com/doc/315770725/IOS-10-Beta-Release-Notes
第 10 页,共 18 页,
An MPMediaPickerController object may not display as expected.
我花了很多时间想自己解决这个问题,但没有成功。
有什么建议吗?
执行以下步骤:
- 将 'NSAppleMusicUsageDescription' 添加到您的 Info.plist 隐私授权。
- 确保您的音乐应用在 iPhone 中可用。它不会在模拟器中运行。
根据我们对评论的讨论,我最近在我的最新应用 named playmates 中使用了 MPMediaPickerController
。我正在与您分享工作代码。我已经编写了不言自明的代码。
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)
}
}
我发现您的代码存在以下差异:
- 使用
self.present
而不是self.presentViewController
- 无需创建
mediaPicker
的全局实例。与在委托方法中一样,您正在获取MPMediaPickerController
的实例
添加隐私 - info.plist
中的媒体库使用说明