AVAudioPlayer无法从网站播放m4a或mp3文件类型 [英] AVAudioPlayer not playing m4a or mp3 filetype from a website

查看:165
本文介绍了AVAudioPlayer无法从网站播放m4a或mp3文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的应用程序中找到仅纯.m4a声音的URL.我有音频的URL,并且可以从理论上下载它.然后,使用下载的文件URL发出声音,我尝试使用AVAudioPlayer播放它,但是它不播放任何声音.这是我的代码:

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:

在URL检索功能中,我调用:(定义为URL(string: url)的URL,URL为检索URL字符串)

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

downloadSound(url: urls!)

这是我的downloadSound()函数:

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

最后是playSound函数:

And lastly the playSound function:

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

一切都被调用,因为print("The url is \(url)")返回文件的路径(但是,我实际上无法跟踪文件).

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

谢谢.

推荐答案

我遇到了同样的问题,并且选择了应用文档所说的替代解决方案:

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

临时文件的文件URL.由于该文件是临时文件,因此您 必须打开文件进行读取或将其移动到永久文件 在返回之前,将其定位在您应用的沙箱容器目录中 通过此委托方法.

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.

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

The idea is just to copy from tmp directory to document directory and play from document directory.

创建一个成员变量:

var player = AVAudioPlayer()

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

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.

这篇关于AVAudioPlayer无法从网站播放m4a或mp3文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆