0

我正在尝试从 parse.com 播放音频。我正在获取 pffile 但无法播放音频。

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()
        }
    }

提前致谢

4

1 回答 1

1

我找到了答案。

早些时候我使用的是 AVAudioPlayer,这就是我收到错误的原因。现在我正在使用 AVPlayer,我的代码运行良好。

var avAudioPlayer: AVPlayer? // 在类中声明

  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)
        }
    }
 }

有趣的 playWithUrl(网址:字符串){

 self.avAudioPlayer =  AVPlayer(URL: NSURL(string: url!)!)
 avAudioPlayer?.play()

}

于 2015-11-26T06:08:10.170 回答