【问题标题】:Swift: Fail to play audio file after downloading from a url in avaudioplayerSwift:从avaudioplayer中的url下载后无法播放音频文件
【发布时间】:2019-01-15 22:52:39
【问题描述】:

我想从 url 播放音频文件。我想在 swift4 和 xcode 9 中使用 avaudioplayer 播放这个。所以,首先我在会话中下载这个并播放那个音频。但有时 avaudioplayer 无法播放音频并显示错误。

代码:

func downloadFileFromURL(url: URL){

    var downloadTask:URLSessionDownloadTask
    downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: { [weak self](URL, response, error) -> Void in

        print("audio download response: \(String(describing: response))")

       SVProgressHUD.dismiss()

        if response != nil {

            if error != nil {

                print("audio file download error: \(String(describing: error))")

                return
            }

            DispatchQueue.main.async {

                self?.play(url: URL!)
            }

        } else {

            SVProgressHUD.dismiss()
            Config.shared.show_simple_alert(title: "Fail", message: "Sorry, Fail to play this music. Plase try another or later", context: self!)
        }
    })

    downloadTask.resume()

}

func play(url: URL) {

    print("playing \(url)")

    do {

        player = try AVAudioPlayer(contentsOf: url)
        player.prepareToPlay()
        player.volume = 1.0
        player.play()

    } catch let error as NSError {

        print("playing error: \(error.localizedDescription)")

    } catch {

        print("AVAudioPlayer init failed")
    }
}


错误 - 播放错误:操作无法完成。 (OSStatus 错误 2003334207。)

【问题讨论】:

  • 你找到解决办法了吗?
  • 对不起,还没有兄弟:(

标签: ios xcode swift3 swift4 xcode9


【解决方案1】:
func downloadFileFromURL(url: String)  {

    if let audioUrl = URL(string: url) {

        // then lets create your document folder url
        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        // lets create your destination file url
        let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
        print(destinationUrl)

        // to check if it exists before downloading it
        if FileManager.default.fileExists(atPath: destinationUrl.path) {
            print("The file already exists at path")
            self.play(url: destinationUrl)
        } else {

            // you can use NSURLSession.sharedSession to download the data asynchronously
            URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) -> Void in
                guard let location = location, error == nil else { return }
                do {
                    // after downloading your file you need to move it to your destination url
                    try FileManager.default.moveItem(at: location, to: destinationUrl)

                    self.play(url: destinationUrl)
                    print("File moved to documents folder")
                } catch let error as NSError {
                    print(error.localizedDescription)
                }
            }).resume()
        }
    }
}

【讨论】:

  • 聪明的回答。这也可以处理多个下载相同的文件:)
  • 很棒,比缓存更好。
【解决方案2】:

最终的解决方案是我使用 avaudio 播放器从 url 播放。但是如果我只使用 avplayer 就可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多