Ajax jQuery Ajax jQuery - 1 month ago 7
Swift Question

Trying to play audio with Swift AVPlayer

Here's my current ViewController:

import UIKit
import AVFoundation

class SecondViewController: UIViewController {
override func viewDidLoad() {
var player = AVPlayer()
let url = "http://www.nasa.gov/mp3/640149main_Computers%20are%20in%20Control.mp3"
let playerItem = AVPlayerItem( URL:NSURL( string:url ) )
player = AVPlayer(playerItem:playerItem)
player.rate = 1.0;
player.play()
}
}


When I run this in the simulator I get no audio, could it be that the iOS sim doesn't play audio, or an issue with my code?

Answer

Move the line:

    var player = AVPlayer()

... so that it is before the line:

override func viewDidLoad() {
Comments