gpt4 book ai didi

ios - 如何使用 Swift 播放 mp3 url?

转载 作者:行者123 更新时间:2023-11-29 05:29:01 24 4
gpt4 key购买 nike

我想使用 Swift 播放 mp3.url。

有没有办法在iOS应用程序中使用http url?或任何其他方式来做到这一点?

请问我想使用 Swift 播放 mp3.url。

func initPlay(){

let url = "http://61.80.90.148/mp3-1.mp3"
audioFile = NSURL(string: url) as URL?
//error
do {
audioPlayer = try AVAudioPlayer(contentsOf: audioFile)
} catch let error as NSError {
print("error: \(error)")
}
}
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
2019-09-09 10:27:40.046265+0900 Aircon[27673:3377166] Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

最佳答案

我认为你不能使用直接 url 来播放音乐,使用 Alamofire 转换为数据。

    Alamofire.download(URL.init(string: "http://61.80.90.148/mp3-1.mp3")!).responseData { (response) in
if let value = response.result.value {
do {
self.audioPlayer = try AVAudioPlayer.init(data: value)
}catch {
// Error Handling
}
}
}

关于ios - 如何使用 Swift 播放 mp3 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57846990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com