你如何在Swift中添加背景音乐

时间:2015-03-10 02:35:22

标签: swift

我想知道如何在Swift中为我正在进行的项目添加背景音乐

1 个答案:

答案 0 :(得分:4)

您可以使用AVAudioPlayer

let soundFilePath = NSBundle.mainBundle().pathForResource("mySound", ofType: "mySound")
let soundFileURL = NSURL(fileURLWithPath: soundFilePath!)
let player = AVAudioPlayer(contentsOfURL: soundFileURL, error: nil)
player.numberOfLoops = -1 //infinite
player.play()

不要忘记在项目中import AVFoundation

相关问题