如何在 Swift4 上从服务器播放 MP3 声音



我已经测试了这个功能,错误如下:

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

我已经在真正的设备 iphone 5 ios 10.3 和模拟器上进行了测试

URL

的初始值设定项fileURLWithPath仅适用于本地文件系统中的 URL,对于远程 URL,您必须使用 API

let sound = URL(string: soundUrl)

基本上不会从远程 URL 同步加载数据。 Use URLSession / URLSessionDataTask .

最新更新