如何使用Swift 3 iOS10为MPMediaPickerControllerDelegate提供一个视图控制器来展示音乐库?

8

我试图遵循最近一篇关于使用MPMediaPickerControllerDelegate来呈现音乐选择列表的帖子。

这个教程可以在以下网址找到:

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行中提到:

MPMediaPickerController对象可能无法按预期显示。

我已经花费了大量时间尝试自己解决这个问题,但没有成功。

有什么建议吗?


你的链接是beta文档的镜像。你应该始终使用来自Apple官方的文档。iOS 10已经发布并退出了beta版。 - JAL
@George Lee,我最近在appstore上发布了一个应用程序,它使用Swift 3为MPMediaPickerControllerDelegate提供了一个带有视图控制器的音乐库。如果你有同样的需求,我可以提供详细的帮助(包括代码)。 - pkc456
@pkc456 那太好了。你能在这篇帖子上分享代码吗? - George Lee
@GeorgeLee,我已经在下面的答案中编写了可工作的代码。我还提到了你代码中的一些不一致之处。我很乐意听取意见。 - pkc456
3个回答

23

按照以下步骤进行:

  1. 在您的 Info.plist 中添加“NSAppleMusicUsageDescription”,以获取隐私权限。
  2. 确保您的 iPhone 上可用音乐应用程序。模拟器中无法使用。

在这个问题的代码中,plist键“NSAppleMusicUsageDescription”并不是必要的,以使其正常运行。 - JAL
3
在新的iOS10系统中,打开时会出现一个空白视图。已在iPhone6 Plus, iOS10上进行测试。 - jhd
这个答案是正确的。在info.plist中添加NSAppleMusicUsageDescription确实允许MPMediaPickerDelegate显示音乐列表。它会弹出一个隐私-媒体库使用类型为字符串,值为Yes。 - George Lee

5
根据我们在评论中的讨论,我最近在我的最新应用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的实例

这仍然不允许我像在iOS10中一样选择歌曲(弹出歌曲列表以供选择)。除非我没有正确按照您的说明操作。 - George Lee
1
NSAppleMusicUsageDescription 添加到您的 Info.plist 文件中。 - pkc456

0
在 info.plist 中添加隐私-媒体库使用描述。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接