SWIFT - 无法设置 MPMediaPicker 委托
SWIFT - Can't set MPMediaPicker delegate
代码:
import UIKit
import MediaPlayer
class WelcomeView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.Music)
mediaPicker.allowsPickingMultipleItems = false
mediapicker1 = mediaPicker
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() }
var mediapicker1 = MPMediaPickerController()
@IBAction func selectsong(sender: AnyObject) {
self.presentViewController(mediapicker1, animated: true, completion: nil)
}
}
didPickMediaItems 是委托方法/如何将委托设置为 mediapicker?在网站上它写得很简单,但我试过了,它在 swift 中不起作用。还有,选歌后怎么播放呢?
我用这个网站来创建它:
media picker
以及一些来自苹果官方编程指南的关于创建 "hello world" 音乐应用程序的代码)以播放来自 allsongsquery()
的音乐
您需要将 MPMediaPickerControllerDelegate
协议添加到您的 UIViewController
class:
class WelcomeView: UIViewController, MPMediaPickerControllerDelegate {
然后你可以将你的 MPMediaPickerController
的委托设置为 self
:
mediaPicker.delegate = self
代码:
import UIKit
import MediaPlayer
class WelcomeView: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.Music)
mediaPicker.allowsPickingMultipleItems = false
mediapicker1 = mediaPicker
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() }
var mediapicker1 = MPMediaPickerController()
@IBAction func selectsong(sender: AnyObject) {
self.presentViewController(mediapicker1, animated: true, completion: nil)
}
}
didPickMediaItems 是委托方法/如何将委托设置为 mediapicker?在网站上它写得很简单,但我试过了,它在 swift 中不起作用。还有,选歌后怎么播放呢?
我用这个网站来创建它: media picker 以及一些来自苹果官方编程指南的关于创建 "hello world" 音乐应用程序的代码)以播放来自 allsongsquery()
的音乐您需要将 MPMediaPickerControllerDelegate
协议添加到您的 UIViewController
class:
class WelcomeView: UIViewController, MPMediaPickerControllerDelegate {
然后你可以将你的 MPMediaPickerController
的委托设置为 self
:
mediaPicker.delegate = self