15
如何使用Swift播放声音?
我想用Swift播放声音。 我的代码在Swift 1.0中有效,但现在在Swift 2或更高版本中不再有效。 override func viewDidLoad() { super.viewDidLoad() let url:NSURL = NSBundle.mainBundle().URLForResource("soundName", withExtension: "mp3")! do { player = try AVAudioPlayer(contentsOfURL: url, fileTypeHint: nil) } catch _{ return } bgMusic.numberOfLoops = 1 bgMusic.prepareToPlay() if (Data.backgroundMenuPlayed == 0){ player.play() Data.backgroundMenuPlayed = 1 } }
148
ios
swift
avfoundation