AVAudioPlayer not playing m4a or mp3 filetype from

2020-05-08 05:56发布

问题:

I am trying to locate a URL which is only pure .m4a sound with my application. I have the URL to the audio and theoretically download it. Then, with the downloaded fileURL to the sound, I try to play it with the AVAudioPlayer, yet it does not play any sound. Here is my code:

In the URL retrieval function, I call: (urls defined as a URL(string: url), url being the retrieve URL string)

downloadSound(url: urls!)

Here is my downloadSound() function:

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:

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

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.

回答1:

I had the same problem and I choosed an alternative solution as app doc said:

A file URL for the temporary file. 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.

Create a member variable:

var player = AVAudioPlayer()

Now implement your downloadSound method as below:

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