从音乐应用程序库中挑选歌曲并播放-Swift 2.0 [英] Picking a song and play from Music app library - Swift 2.0

查看:130
本文介绍了从音乐应用程序库中挑选歌曲并播放-Swift 2.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚参加了基本的Swift 2.0课程.我正在尝试制作一个应用程序,以便从iOS的音乐应用程序库中选择一首歌曲并播放.我碰到了这个链接,其中显示了如何制作媒体项目选择器.

I just took a basic Swift 2.0 course. I am trying to make an app to select a song from iOS's Music app library and play it. 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行显示

无法将类型"ViewController"的值分配给类型 "MPMediaPickerControllerDelegate?"

Cannot assign value of type 'ViewController' to type 'MPMediaPickerControllerDelegate?'

错误消息.当我阻止它时,该应用程序可以运行,并且可以让我完美地浏览歌曲.

error message. When I blocked it, the app works and allow me to browse songs perfectly.

问题1:我想知道此行的用途是什么?

Question 1: I would like to know what is the use of this line?

问题2:如何播放使用此代码挑选的歌曲?

Question 2: How to play a song that I picked using this code?

我在这里和其他网站上搜索了如何播放歌曲.我发现人们正在使用 player.play()播放音乐.我尝试过但失败了.

I searched here and other websites for how to play songs. I found people are using player.play() to play music. I tried that and failed.

推荐答案

ViewController需要符合'MPMediaPickerControllerDelegate':

ViewController needs to conform to the '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: {})
 }

添加以下方法以符合MPMediaPickerControllerDelegate:

Add these methods to conform to 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")
 } 

'mediaPicker.delegate = self' 

用于设置ViewController以响应上面添加的功能.如果您未设置委托,那么mediaPicker仍然会出现,但是您的ViewController不会知道用户已执行操作.

is to setup ViewController to respond to the functions added above. If you don't set the delegate the mediaPicker will still present, but your ViewController won't know the user made an action.

无论何时设置委托,请确保该类符合委托方法.如果您不知道这些方法,请在Apple的Developer文档中搜索该委托(即搜索"MPMediaPickerControllerDelegate"),然后会看到可以添加的所有委托方法.

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.

这篇关于从音乐应用程序库中挑选歌曲并播放-Swift 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆