【发布时间】:2016-12-12 01:19:39
【问题描述】:
我正在尝试在应用启动后播放音乐。没有显示构建错误,但没有播放音乐。这是我在 viewDidLoad 中的代码。我还在顶部导入了 AVFoundation 和 SpriteKit。 (类似问题中提出的解决方案与 Swift 2 相关,不适用于 Swift 3)
override func viewDidLoad() {
//这里我改变了datepicker的背景颜色
dateWheel.backgroundColor = .blue
dateWheel.setValue(UIColor.white, forKeyPath: "textColor")
//应用启动后音乐应该开始播放
func playSound(soundName: String)
{
let musicPlay = NSURL(fileURLWithPath: Bundle.main.path(forResource: "chinaAudioNew", ofType: "mp3")!)
do{
let audioPlayer = try AVAudioPlayer(contentsOf:musicPlay as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
audioPlayer.numberOfLoops = -1
}catch {
print("Error getting the audio file")
}
}
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】:
-
这是 swift 3,我尝试了这些解决方案,但声音仍然没有播放
标签: audio swift3 avfoundation avaudioplayer