我测试了这个函数,错误如下:
playSound(soundUrl: "httpwebxxxxxxxx/sound2.mp3")
func playSound(soundUrl: String) {
let sound = URL(fileURLWithPath: soundUrl)
do {
let audioPlayer = try AVAudioPlayer(contentsOf: sound)
audioPlayer.prepareToPlay()
audioPlayer.play()
}catch let error {
print(soundUrl)
print("Error: \(error.localizedDescription)")
//Error: The operation couldn’t be completed. (OSStatus error 2003334207.)
}
}
我已经在realy设备iphone 5 ios 10.3和模拟器上进行了测试
答案 0 :(得分:0)
URL的初始化程序fileURLWithPath
仅适用于本地文件系统中的URL,对于必须使用API的远程URL
let sound = URL(string: soundUrl)
基本上不要从远程URL同步加载数据。 Use URLSession / URLSessionDataTask
。