-1

I have two view controllers, one with mediapicker and one with the actual player. so i done this code:

first view

import UIKit
import MediaPlayer

class WelcomeView: UIViewController, MPMediaPickerControllerDelegate {

override func viewDidLoad() {
super.viewDidLoad()
var mediaPicker: MPMediaPickerController = MPMediaPickerController.self(mediaTypes:MPMediaType.Music)
mediaPicker.allowsPickingMultipleItems = false
mediapicker1 = mediaPicker
mediaPicker.delegate = self
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning() }

var mediapicker1 = MPMediaPickerController()

@IBAction func selectsong(sender: AnyObject) {
self.presentViewController(mediapicker1, animated: true, completion: nil)
}

func mediaPicker(mediaPicker: MPMediaPickerController!, didPickMediaItems mediaItemCollection: MPMediaItemCollection!) {
    self.dismissViewControllerAnimated(true, completion: nil)
    println("you picked: \(mediaItemCollection)")
    selectedelement = mediaItemCollection

    let vc = MainView(nibName: "MainView", bundle: nil)
    navigationController?.pushViewController(vc, animated: true) 
}

var selectedelement = MPMediaItemCollection()
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var DestView: MainView = segue.destinationViewController as MainView
    DestView.selectedsong = selectedelement
 }}

and the player view

import UIKit
import MediaPlayer

class MainView: UIViewController {

var play = MPMusicPlayerController()
var selectedsong = MPMediaItemCollection()

override func viewDidLoad() {
super.viewDidLoad()

    func prepareToPlay() -> Bool{
    let myplayer = MPMusicPlayerController.applicationMusicPlayer()
    myplayer.setQueueWithItemCollection(selectedsong)
    play = myplayer
    myplayer.play()
    return true
    }}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}}

but when i tap start the xcode gives me this error: "MPMediaItemCollectionInitException', reason: '-init is not supported, use -initWithItems:"

I searched on google about this, and found something like "I not actually running the query in the background"

so i dont realy understand what to do))

1 Answers1

0

The issue is that you're initializing your variables instead of just declaring them and your choice of initializer is not valid.

var selectedsong = MPMediaItemCollection()
//       missing "items:" in constructor ^

Since you're setting selectedsong in your prepareForSegue anyways, you should simply declare it and not try to initialize a value:

var selectedsong : MPMediaItemCollection

Side note: the same is true of play. You are resetting its value in prepareToPlay before you use it for the first time, so this initial value is thrown away every time anyway.

var play : MPMusicPlayerController
Ian MacDonald
  • 13,472
  • 2
  • 30
  • 51