尝试使用Swift AVPlayer播放音频

时间:2015-08-22 02:41:31

标签: ios swift audio avplayer

这是我当前的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()
    }
}

当我在模拟器中运行时,我没有音频,可能是iOS sim没有播放音频,或者我的代码有问题吗?

1 个答案:

答案 0 :(得分:6)

移动线:

    var player = AVPlayer()

...所以之前行:

override func viewDidLoad() {