-1

I wanna build my first macOS-App. The aim is, that I can select a radiostream and play this. But it doesn't work.

Here is the code:

import Cocoa
import AVFoundation
import AVKit

class ViewController: NSViewController {

@IBOutlet weak var RadioList: NSPopUpButton!

override func viewDidLoad() {
    super.viewDidLoad()

    let RadioListArray = ["Unser Radio", "Antenne"]

    RadioList.removeAllItems()
    RadioList.addItems(withTitles: RadioListArray)
    RadioList.selectItem(at: 0)
}

override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}

@IBAction func PlayStopButton(_ sender: AnyObject) {

    var url = String()

    switch RadioList.indexOfSelectedItem {
    case 0:
        url = "http://rs4.stream24.net:80/unser-radio.mp3"
    case 1:
        url = "http://mp3channels.webradio.antenne.de/antenne"
    default:
        print("nothing")
    }
    Stream(url: url)
}

func Stream(url: String)
{
    print(url)

    let player = AVPlayer(url: URL(string: url)!)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = self.view.bounds
    self.view.layer?.addSublayer(playerLayer)
    player.play()
}
}

When I copy the url's direct to the browser, than the streaming start well. When I insert a Video, then the video starts in the application.

Is der something special for audiostreaming?

1 Answers1

0

it is because apple is not allowing you to load any http contents by default. if you looks closely on debug console you might have got this below error

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.

To solve it just add below to your info.plist file

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

Detailed info can be found in this SO answer

Community
  • 1
  • 1
Bluewings
  • 3,438
  • 3
  • 18
  • 31