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?