简体   繁体   中英

Why am I getting a nil error?

Currently I can add the current playing song on the iPhone to my app, but I am trying to play a song from Parse based on the song title on the app, but I am getting a nil error. Why?

 func playit(sender: UIButton!){
    if let nowPlaying = musicPlayer.nowPlayingItem{
    let title = nowPlaying[MPMediaItemPropertyTitle] as? String
    let artist = nowPlaying[MPMediaItemPropertyTitle] as? String

    println(title! + artist!)


        let query = PFQuery(className: "Songs")
        query.whereKey("SongName", equalTo: title!)
        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
                // The find succeeded.
                println("Successfully retrieved \(objects!.count) song(s).")
                // Do something with the found objects
                if let objects = objects as? [PFObject] {
                    for object in objects {
                        println(object.objectId)

                        let objects: PFObject = object as PFObject
                        let parseAudio: PFFile = objects.valueForKey("Songs") as! PFFile
                        let audioPath: String = parseAudio.url!
                        let urlParse: NSURL = NSURL(string: audioPath)!

                        player = AVPlayer(URL: urlParse)
                        println(player)
                        player.play()
                    }
                }
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
            }

        }

    }
}

Without knowing what the exact error is I can only say that your issues most likely resides in this block:

            let objects: PFObject = object as PFObject
                    let parseAudio: PFFile = objects.valueForKey("Songs") as! PFFile
                    let audioPath: String = parseAudio.url!
                    let urlParse: NSURL = NSURL(string: audioPath)!

Converting parseAudio to a string for your NSURL isn't getting the content of the file for your media player. Try something like this:

let parseAudio: PFFile = objects.valueForKey("Songs") as? PFFile
parseAudio?.getDataInBackgroundWithBlock({ (data, error) -> Void in

      if error == nil {
             // do what you need to with the music file which is contained in `data`
              let fullParseAudioFile = data

      } else if error != nil {

      println(error!.localizedDescription)

      }

  })

I haven't worked with the media player so I don't know what format you'll have to cast data to but you would now have the usable file instead of just a string representing the name of the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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