Reputation: 636
i am trying to play audio from parse.com. I am getting the pffile but unable to play audio.
func testing() {
let query = PFQuery(className: "Attractions")
query.whereKey("objectId", equalTo: "hDyP0SwbAQ")
query.findObjectsInBackgroundWithBlock { (result, error) -> Void in
for obj in result! {
let path = obj.objectForKey("attraction_file") as! PFFile
self.playWithURL(path)
}
}
}
func playWithURL(url:PFFile) {
do {
let audioPlayer = try! AVAudioPlayer(contentsOfURL: url)
audioPlayer.prepareToPlay()
audioPlayer.play()
}
}
Thanks In Advance
Upvotes: 0
Views: 111
Reputation: 636
I Found Answer.
earlier i was using AVAudioPlayer thats why i was getting the error. now i am using AVPlayer and my code is working perfect.
var avAudioPlayer: AVPlayer? // declared in class
func testing() {
let query = PFQuery(className: "Attractions")
query.whereKey("objectId", equalTo: "hDyP0SwbAQ")
query.findObjectsInBackgroundWithBlock { (result, error) -> Void in
for obj in result! {
let path = obj.objectForKey("SongFile") as! PFFile
self.playWithURL(path)
}
}
}
fun playWithUrl(url : String) {
self.avAudioPlayer = AVPlayer(URL: NSURL(string: url!)!)
avAudioPlayer?.play()
}
Upvotes: 1