我今天开始学习。我有一个应用程序,按下按钮时会播放多个声音。在我的viewController.swift里面,我有
func playSound( fileName: String ){
// Set the sound file name & extension
var bangBangSound = NSURL(
fileURLWithPath: NSBundle.mainBundle().pathForResource("myFile", ofType: "mp3" )!)
// preparation to play the sound
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil )
AVAudioSession.sharedInstance().setActive( true, error: nil )
// play the sound
var error: NSError?
audioPlayer = AVAudioPlayer( contentsOfURL: bangBangSound, error: &error )
audioPlayer.prepareToPlay()
audioPlayer.play()
}
@IBAction func playCheefKeef(sender: AnyObject) {
playSound( "myFile" )
}
但它没有播放声音,当我没有调用playSound并且我在@IBAction方法中有代码时它工作正常。我该如何解决这个问题?