[iOS Swift流音频使用AVFoundation从喊话或冰河广播

问题描述 投票:0回答:1

我正在尝试创建一个从Shoutcast或Icecast获取音频的广播应用。我已经能够从以mp3结尾的http网址播放音乐(例如: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个回答
0
投票

查看日志后;我发现了问题。如果使用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,并且可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.