Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio live streaming Swift

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.

like image 873
Farid Al Haddad Avatar asked Feb 28 '17 21:02

Farid Al Haddad


1 Answers

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)
    }
}
like image 71
Farid Al Haddad Avatar answered Sep 19 '22 21:09

Farid Al Haddad