简体   繁体   English

AVAudioPlayer 不播放来自网站的 m4a 或 mp3 文件类型

[英]AVAudioPlayer not playing m4a or mp3 filetype from a website

I am trying to locate a URL which is only pure .m4a sound with my application.我正在尝试使用我的应用程序找到一个只有纯 .m4a 声音的 URL。 I have the URL to the audio and theoretically download it.我有音频的 URL,理论上可以下载它。 Then, with the downloaded fileURL to the sound, I try to play it with the AVAudioPlayer, yet it does not play any sound.然后,使用下载的 fileURL 到声音,我尝试使用 AVAudioPlayer 播放它,但它不播放任何声音。 Here is my code:这是我的代码:

In the URL retrieval function, I call: (urls defined as a URL(string: url) , url being the retrieve URL string)在 URL 检索功能中,我调用:(定义为URL(string: url) url,url 是检索 URL 字符串)

downloadSound(url: urls!)

Here is my downloadSound() function:这是我的 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()
}

And lastly the playSound function:最后是 playSound 函数:

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

Everything is being called as the print("The url is \\(url)") returns me the path of the file (I am not actually able to track the file, however).一切都被称为print("The url is \\(url)")返回文件的路径(但是,我实际上无法跟踪文件)。

Here is the general path of the sound on the simulator:下面是模拟器上声音的大致路径:

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

Whereas running it on a phone returns:而在手机上运行它会返回:

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

Thank you in advance.提前谢谢你。

I had the same problem and I choosed an alternative solution as app doc said:我遇到了同样的问题,我选择了应用程序文档所说的替代解决方案:

A file URL for the temporary file.临时文件的文件 URL。 Because the file is temporary, you must either open the file for reading or move it to a permanent location in your app's sandbox container directory before returning from this delegate method.由于文件是临时文件,因此在从此委托方法返回之前,您必须打开文件进行读取或将其移动到应用程序沙箱容器目录中的永久位置。

The idea is just to copy from tmp directory to document directory and play from document directory.这个想法只是从 tmp 目录复制到文档目录并从文档目录播放。

Create a member variable:创建成员变量:

var player = AVAudioPlayer()

Now implement your downloadSound method as below:现在实现您的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()
}

This is just a sample solution.这只是一个示例解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM