0

Using AVPlayer in Swift. No error but no sound playing on iPhone.

I'm new to Swift. I will appreciate a response.

Here is my code:

import UIKit
import AVFoundation

class PlayerViewController: UIViewController {
    // This time, we'll declare avPlayer as an instance variable,
    // which means it exists as long as our view controller exists.
    var avPlayer: AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("Connecting...")

        // If you'd rather not use this sound file,
        // replace the string below with the URL of some other MP3.
        let urlString = "http://live.canstream.co.uk:8000/bangradio.mp3"
        let url = NSURL(string: urlString)!
        avPlayer = AVPlayer(URL: url)

        print("Connected to One Radio!")
        avPlayer.play()
        print("Now playing...")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Don P
  • 11
  • 2
  • Hi and welcome to Stack Overflow. What have you tried so far in terms of debugging? For example are you sure that you are not on mute? Are you trying this with the Xcode Simulator etc. – tech4242 Sep 27 '16 at 17:44
  • I bet iOS 9 App Transport Security feature is blocking your http request. See here for solutions: http://stackoverflow.com/q/31254725/22147 – Rhythmic Fistman Sep 27 '16 at 21:40
  • This was not the problem for me and I still cannot listen to the music. any solution? – Pavlos Jan 30 '18 at 10:35
  • I had the same problem this answer worked to fix it: https://stackoverflow.com/a/34735386/4833705 – Lance Samaria Mar 12 '18 at 18:55

1 Answers1

0

You would try to use AVPlayerItem(url:"your url"), and set into avplayer.

cleexiang
  • 149
  • 2
  • 7