简体   繁体   English

从音乐应用程序库中挑选歌曲并播放-Swift 2.0

[英]Picking a song and play from Music app library - Swift 2.0

I just took a basic Swift 2.0 course. 我刚刚参加了基本的Swift 2.0课程。 I am trying to make an app to select a song from iOS's Music app library and play it. 我正在尝试制作一个应用程序,以便从iOS的音乐应用程序库中选择一首歌曲并播放。 I came across this link which shows how to make media item picker. 我碰到了这个链接 ,该链接显示了如何制作媒体项目选择器。

import UIKit
import MediaPlayer

class ViewController: UIViewController {

@IBOutlet weak var pickSong: UIButton!

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.

  let mediaPicker = MPMediaPickerController(mediaTypes: .Music)

  // mediaPicker.delegate = self
  // mediaPicker.prompt = "Select song (Icloud songs must be downloaded to use)"
  mediaPicker.allowsPickingMultipleItems = false
  mediaPicker.showsCloudItems = false
  presentViewController(mediaPicker, animated: true, completion: {})
}

mediaPicker.delegate = self line shows mediaPicker.delegate = self显示

Cannot assign value of type 'ViewController' to type 'MPMediaPickerControllerDelegate?' 无法将类型“ ViewController”的值分配给类型“ MPMediaPickerControllerDelegate?”

error message. 错误信息。 When I blocked it, the app works and allow me to browse songs perfectly. 当我阻止它时,该应用程序可以运行,并且可以让我完美地浏览歌曲。

Question 1: I would like to know what is the use of this line? 问题1:我想知道此行的用途是什么?

Question 2: How to play a song that I picked using this code? 问题2:如何播放使用此代码挑选的歌曲?

I searched here and other websites for how to play songs. 我在这里和其他网站上搜索了如何播放歌曲。 I found people are using player.play() to play music. 我发现人们正在使用player.play()播放音乐。 I tried that and failed. 我尝试过但失败了。

ViewController needs to conform to the 'MPMediaPickerControllerDelegate': ViewController需要符合“ MPMediaPickerControllerDelegate”:

//Let other classes know ViewController is a MPMediaPickerControllerDelegate
class ViewController: UIViewController, MPMediaPickerControllerDelegate {

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let mediaPicker = MPMediaPickerController(mediaTypes: .Music)
    mediaPicker.delegate = self
    presentViewController(mediaPicker, animated: true, completion: {})
 }

Add these methods to conform to MPMediaPickerControllerDelegate: 添加以下方法以符合MPMediaPickerControllerDelegate:

 func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {

    //User selected a/an item(s). 
    for mpMediaItem in mediaItemCollection.items {
      print("Add \(mpMediaItem) to a playlist, prep the player, etc.")
    }
 }

 func mediaPickerDidCancel(mediaPicker: MPMediaPickerController) {
    print("User selected Cancel tell me what to do")
 } 

The purpose of 的目的

'mediaPicker.delegate = self' 

is to setup ViewController to respond to the functions added above. 是设置ViewController来响应上面添加的功能。 If you don't set the delegate the mediaPicker will still present, but your ViewController won't know the user made an action. 如果不设置委托,mediaPicker仍然会出现,但是ViewController不会知道用户已执行操作。

Whenever you set a delegate, make sure the class conforms to the delegate methods. 每当您设置委托时,请确保该类符合委托方法。 If you don't know the methods, search through Apple's Developer docs for that delegate (ie search for 'MPMediaPickerControllerDelegate') and you'll see all the delegate methods you can add. 如果您不知道这些方法,请在Apple的Developer文档中搜索该委托(即搜索“ MPMediaPickerControllerDelegate”),然后会看到可以添加的所有委托方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM