【问题标题】:iOS Swift stream audio from shoutcast or icecast using AVFoundationiOS Swift 使用 AVFoundation 从shoutcast 或icecast 流式传输音频
【发布时间】:2020-09-14 14:25:41
【问题描述】:
我正在尝试创建一个从 Shoutcast 或 Icecast 获取音频的广播应用。我已经能够从以 mp3 结尾的 http url 播放音乐(例如:https://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3)。我无法从这样的网址流式传输(例如:http://hydra.shoutca.st:8057/autodj)。一定有我遗漏的东西。
import UIKit
import AVFoundation
class ViewController: UIViewController {
var player: AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
//https://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3 - this works
let url = URL(string: "http://edge2-b.exa.live365.net/a36007")
player = AVPlayer(url: url!)
}
@IBAction func playButtonPressed(_ sender: Any) {
player.play()
}
@IBAction func stopButtonPressed(_ sender: Any) {
player.pause()
}
}
【问题讨论】:
标签:
ios
streaming
swift4
shoutcast
【解决方案1】:
这可能有点晚回答,但是,您可以通过在 info.plist 文件中添加值来解决 http 传输阻塞。查看此 Apple 文档NSAppTransport Security。您所要做的就是将bool 的Allow Arbitrary Loads 设置为true
【解决方案2】:
查看日志后;我发现了问题。如果您使用 http 但要播放 mp3 文件,它将播放。如果您没有,那么您需要对 Shoutcast 和 Icecast 等流使用 https。这是日志所说的。
2020-05-27 10:42:11.562431-0400 HotcueRadio[5134:272722] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2020-05-27 10:42:11.562576-0400 HotcueRadio[5134:272722] Cannot start load of Task <7AF598D9-A7AD-438C-99A1-8095A1F8CEC7>.<1> since it does not conform to ATS policy
2020-05-27 10:42:11.581133-0400 HotcueRadio[5134:272736] Task <7AF598D9-A7AD-438C-99A1-8095A1F8CEC7>.<1> finished with error [-1022] Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection., NSErrorFailingURLStringKey=http://edge2-b.exa.live365.net/a36007, NSErrorFailingURLKey=http://edge2-b.exa.live365.net/a36007, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <7AF598D9-A7AD-438C-99A1-8095A1F8CEC7>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <7AF598D9-A7AD-438C-99A1-8095A1F8CEC7>.<1>, NSUnderlyingError=0x60000011c510 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}}
所以我在网址中添加了 https 并且它可以工作。