user3225094
user3225094

Reputation: 81

iOS Swift stream audio from shoutcast or icecast using AVFoundation

I'm trying to create a radio app that gets audio from Shoutcast or Icecast. I've been able to play music from a http url that ends in mp3 (ex: https://soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3). I can not stream from a url like this (ex: http://hydra.shoutca.st:8057/autodj). There has to be something that I'm missing.

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

Upvotes: 1

Views: 1103

Answers (2)

heavyguidence
heavyguidence

Reputation: 373

This might be bit late to answer, but, you can solve the http transport blocking by adding values in info.plist file. Check out this Apple Documentation NSAppTransport Security. All you have to do is set the bool for Allow Arbitrary Loads to true

Upvotes: 1

user3225094
user3225094

Reputation: 81

After looking at the logs; I have found the problem. If you use http but have a mp3 file to play it will play. If you do not have one then you need to use https for streams like Shoutcast and Icecast. Here is what the logs said.

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

So I added https to the url and it works.

Upvotes: 0

Related Questions