如何在Swift中添加背景音乐

如何在Swift中添加背景音乐,swift,Swift,我想知道如何在Swift中为我正在处理的项目添加背景音乐。您可以使用AVAudioPlayer: 不要忘记在项目中导入AVFoundation。欢迎使用StackOverflow!请阅读 let soundFilePath = NSBundle.mainBundle().pathForResource("mySound", ofType: "mySound") let soundFileURL = NSURL(fileURLWithPath: soundFilePath!) let player

我想知道如何在Swift中为我正在处理的项目添加背景音乐。您可以使用AVAudioPlayer:


不要忘记在项目中导入AVFoundation。

欢迎使用StackOverflow!请阅读
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()