Reputation: 833
I need to build an application that plays an audio live streaming URL for a radio station.
The URL is available but i can't find a good way to implement it in my application.
I've tried this way but the player won't provide any sound, although i am sure it is loading the URL (it plays the stream when trying on a simulator but on a real device it does not)
This is my code:
var player : AVPlayer!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let url = "http://listen.shoutcast.com/radiodeltalebanon"
player = AVPlayer(url: URL(string: url)!)
player.volume = 1.0
player.rate = 1.0
player.play()
}
The indicator beside the 4G or the Wifi icon in the status bar is showing while in app and if i turn the volume of my phone up, the Volume is up and not the Ringer.
This means that it is actually playing but no sound is available.
Can anyone help me please ?
Thanks.
Upvotes: 11
Views: 7940
Reputation: 12934
For Swift 4.2:
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
try AVAudioSession.sharedInstance().setActive(true)
} catch {
print(error)
}
}
Upvotes: 0
Reputation: 833
Turned out that i missed this :
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch let error as NSError {
print(error.localizedDescription)
}
} catch let error as NSError {
print(error.localizedDescription)
}
}
Upvotes: 5