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

标签 ios swift xcode

我想使用 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/

相关文章:

iOS Swift webview 重新加载动态构建网站的实际站点

ios - 具有最大长度的 UITextField 自动完成

xcode - 如何诊断 Xcode 错误

ios - 在 UICollectionView 中显示一行 - Swift

ios - 具有相同包标识符的 IPA 的 iPhone 分发是更新?

ios - 如何在 UICollectionViewCell 中显示 UITableView

iOS 应用提交和 Beta 审核流程

ios - 使用 Xcode UI 测试来测试底层框架行为

ios - Codename One 中使用 native code 时参数可以随意命名吗?

ios - 为 UITableView 指定 UITableViewRowAnimationNone 时发生行动画