MPMediaPickerController 在启动后关闭 (Swift)

MPMediaPickerController closes after startup (Swift)

当我单击单元格以打开 MPMediaPickerController 时,它会在启动时打开。

class MediaViewController: UITableViewController, MPMediaPickerControllerDelegate

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if indexPath.section == 2 {
        if indexPath.row == 0 {
           let mediaPicker = MPMediaPickerController(mediaTypes: MPMediaType.anyAudio)
           mediaPicker.delegate = self
           mediaPicker.prompt = "Select song"
           mediaPicker.allowsPickingMultipleItems = false
           self.present(mediaPicker, animated: true, completion: nil)
        }
    }
}

func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems  mediaItemCollection:MPMediaItemCollection) -> Void {
}

func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController) {
    dismiss(animated: true, completion: nil)
    print("MPMediaPickerController - Cancel")
}

请将此添加到您的 info.plist

<key>NSAppleMusicUsageDescription</key>
<string>$(app Name) uses music</string>

对我有用。