Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 532
Ajax jQuery Avatar asked Aug 22 '15 02:08

Ajax jQuery


1 Answers

Move the line:

    var player = AVPlayer()

... so that it is before the line:

override func viewDidLoad() {
like image 84
matt Avatar answered Sep 21 '22 20:09

matt