Xcode 7.1 - swift2.0 - parse.com
收到错误:
模糊地使用'url'
if let AudioFileURLTemp = object?.valueForKey("SongFile")?.url {
player = AVPlayer(URL: NSURL(string: AudioFileURLTemp!))
player.play()
}
请告知
答案 0 :(得分:0)
无需使用NSURL或字符串转换。直接访问PFFile
网址
if let songFile: PFFile = object?.valueForKey("SongFile") as? PFFile {
player = AVPlayer(URL: songFile.url!)
player.play()
}
答案 1 :(得分:0)
感谢您的帮助,这是我用于swift 2的功能
func GetSongFromParse(){
let querySongs = PFQuery(className: "Songs")
querySongs.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
if let objects = objects{
for object in objects{
self.iDArray.append(object.valueForKey("objectId") as! String)
self.songNamesArray.append(object["SongName"] as! String)
self.songFileArray.append(object["SongFile"] as! PFFile)
}
}
print(self.songFileArray[0])
print(self.songNamesArray[0])
print(self.iDArray)
if let url = NSURL(string: self.songFileArray[0].url!){
self.playerItem = AVPlayerItem(URL: url)
self.player = AVPlayer(playerItem: self.playerItem!)
}
}
}
答案 2 :(得分:0)
感谢@Aaron提供以下解决方案
func grabSong(){
let SongQuery = PFQuery(className: "songs")
SongQuery.getObjectInBackgroundWithId(iDArray[SelectedSongNumber], block:{
(object: PFObject?, error:NSError?)-> Void in
if let songFile: PFFile = object?.valueForKey("songFile") as? PFFile {
AudioPlayer = AVPlayer(URL: songFile.url)
AudioPlayer.play()
}
})
}
答案 3 :(得分:0)
在Swift 3中, url 参数是小写的,这解决了我的问题。
player = AVPlayer(url: URL(string: AudioFileURLTemp!))