【问题标题】:AVAudioPlayer not playing m4a or mp3 filetype from a websiteAVAudioPlayer 不从网站播放 m4a 或 mp3 文件类型
【发布时间】:2018-03-20 18:23:37
【问题描述】:

我试图在我的应用程序中找到一个纯 .m4a 声音的 URL。我有音频的 URL,理论上可以下载它。然后,将下载的 fileURL 指向声音,我尝试使用 AVAudioPlayer 播放它,但它不播放任何声音。这是我的代码:

在URL检索函数中,我调用:(urls定义为URL(string: url),url为检索URL字符串)

downloadSound(url: urls!)

这是我的 downloadSound() 函数:

func downloadSound(url:URL){

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

最后是 playSound 函数:

func playSound(url:URL) {
    print("The url is \(url)")
    let player = try! AVAudioPlayer(contentsOf: url)
    player.play()

一切都被称为 print("The url is \(url)") 返回文件的路径(但我实际上无法跟踪文件)。

这是模拟器上声音的大致路径:

file:///Users/[...]/Library/Developer/CoreSimulator/Devices/116C311A-C7F3-44EC-9762-2FAA0F9FE966/data/Containers/Data/Application/60BFCDE7-AC02-4196-8D1A-24EC646C4622/tmp/CFNetworkDownload_7VDpsV.tmp

而在手机上运行它返回:

file:///private/var/mobile/Containers/Data/Application/C75C1F1D-77E9-4795-9A38-3F0756D30547/tmp/CFNetworkDownload_T1XlPb.tmp

提前谢谢你。

【问题讨论】:

    标签: ios swift url audio avaudioplayer


    【解决方案1】:

    我遇到了同样的问题,我选择了一个替代解决方案,正如 app doc 所说:

    临时文件的文件 URL。因为文件是临时的,你 必须打开文件进行读取或将其移动到永久 返回之前在您应用的沙箱容器目录中的位置 来自这个委托方法。

    想法只是从tmp目录复制到文档目录并从文档目录播放。

    创建成员变量:

    var player = AVAudioPlayer()
    

    现在实现您的 downloadSound 方法,如下所示:

    func downloadSound(url:URL){
        let docUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
        let desURL = docUrl.appendingPathComponent("tmpsong.m4a")
        var downloadTask:URLSessionDownloadTask
        downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: { [weak self](URLData, response, error) -> Void in
            do{
                let isFileFound:Bool? = FileManager.default.fileExists(atPath: desURL.path)
                if isFileFound == true{
                      print(desURL) //delete tmpsong.m4a & copy
    
                    try FileManager.default.removeItem(atPath: desURL.path)
                    try FileManager.default.copyItem(at: URLData!, to: desURL)
    
                } else {
                    try FileManager.default.copyItem(at: URLData!, to: desURL)
                }
                let sPlayer = try AVAudioPlayer(contentsOf: desURL!)
                self?.player = sPlayer
                self?.player.prepareToPlay()
                self?.player.play()
    
            }catch let err {
                print(err.localizedDescription)
            }
                
            })
        downloadTask.resume()
    }
    

    这只是一个示例解决方案。

    【讨论】:

    • 非常感谢您提供此解决方案。我不知道您必须在应用临时文件之前保存它们!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多