[英]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. 目前,我可以在iPhone上将当前正在播放的歌曲添加到我的应用程序中,但是我正在尝试根据应用程序上的歌曲名称从Parse播放一首歌曲,但出现了nil错误。 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. 将parseAudio
转换为NSURL
的字符串不会为媒体播放器获取文件的内容。 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. 我没有与媒体播放器一起工作,所以我不知道将数据转换为哪种格式,但是现在您将拥有可用的文件,而不仅仅是代表文件名的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.