Ajax jQuery Ajax jQuery - 1 year ago 123
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;

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 Source

Move the line:

    var player = AVPlayer()

... so that it is before the line:

override func viewDidLoad() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download