我有4个按钮可以触发4种不同的声音。所有按钮都工作,我有一个println按钮1,按钮2等。由于某种原因,我只听到通过按钮3和4发出的声音。我对所有按钮和音频播放器使用相同的编码方法,所以我不知道问题可能是什么。这是我的代码。
没有任何错误或任何内容,所有按钮和音频播放器的工作方式相同,因此我不确定为什么按钮1和按钮2不起作用。
非常感谢任何帮助。
class MusicPlayer {
var onePlayer : AVAudioPlayer?
var twoPlayer : AVAudioPlayer?
var threePlayer : AVAudioPlayer?
var fourPlayer : AVAudioPlayer?
func playOneSound() {
twoPlayer!.play()
}
func playTwoSound() {
twoPlayer!.play()
}
func playThreeSound() {
threePlayer!.play()
}
func playFourSound() {
fourPlayer!.play()
}
func stopOneSound() {
twoPlayer!.stop()
}
func stopTwoSound() {
twoPlayer!.stop()
}
func stopThreeSound() {
threePlayer!.stop()
}
func stopFourSound() {
fourPlayer!.stop()
}
class var sharedInstance: MusicPlayer {
struct Static {
static var instance: MusicPlayer?
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token) {
Static.instance = MusicPlayer()
}
return Static.instance!
}
init() {
// Create an NSURL for the correct file
let oneSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Go To Sleep 1", ofType: "mp3")!)
let twoSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Go To Sleep 2", ofType: "mp3")!)
let threeSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Go To Sleep 3", ofType: "mp3")!)
let fourSound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Go To Sleep 5", ofType: "mp3")!)
// Create a new AVAudioPlayer with the correct file
onePlayer = AVAudioPlayer(contentsOfURL: oneSound, error: nil)
twoPlayer = AVAudioPlayer(contentsOfURL: twoSound, error: nil)
threePlayer = AVAudioPlayer(contentsOfURL: threeSound, error: nil)
fourPlayer = AVAudioPlayer(contentsOfURL: fourSound, error: nil)
/*onePlayer!.volume = masterEffectSoundLevel
twoPlayer!.volume = masterEffectSoundLevel
threePlayer!.volume = masterEffectSoundLevel
fourPlayer!.volume = masterEffectSoundLevel*/
// Prepare for playback, set loop count to infinite, start playback
onePlayer!.prepareToPlay()
twoPlayer!.prepareToPlay()
threePlayer!.prepareToPlay()
fourPlayer!.prepareToPlay()
}
这是我的按钮代码:
@IBAction func settingOneButton(sender: UIButton) {
settingOne.hidden = false
settingTwo.hidden = true
settingThree.hidden = true
settingFour.hidden = true
}
@IBAction func settingTwoButton(sender: UIButton) {
settingOne.hidden = true
settingTwo.hidden = false
settingThree.hidden = true
settingFour.hidden = true
}
@IBAction func settingThreeButton(sender: UIButton) {
settingOne.hidden = true
settingTwo.hidden = true
settingThree.hidden = false
settingFour.hidden = true
}
@IBAction func settingFourButton(sender: UIButton) {
settingOne.hidden = true
settingTwo.hidden = true
settingThree.hidden = true
settingFour.hidden = false
}
@IBAction func goToSleepPressed(sender: AnyObject) {
if settingOne.hidden == false {
MusicPlayer.sharedInstance.playOneSound()
MusicPlayer.sharedInstance.stopTwoSound()
MusicPlayer.sharedInstance.stopThreeSound()
MusicPlayer.sharedInstance.stopFourSound()
println("one")
} else if settingTwo.hidden == false {
MusicPlayer.sharedInstance.playTwoSound()
MusicPlayer.sharedInstance.stopOneSound()
MusicPlayer.sharedInstance.stopThreeSound()
MusicPlayer.sharedInstance.stopFourSound()
println("two")
} else if settingThree.hidden == false {
MusicPlayer.sharedInstance.playThreeSound()
MusicPlayer.sharedInstance.stopOneSound()
MusicPlayer.sharedInstance.stopTwoSound()
MusicPlayer.sharedInstance.stopFourSound()
println("three")
} else if settingFour.hidden == false {
MusicPlayer.sharedInstance.playFourSound()
MusicPlayer.sharedInstance.stopOneSound()
MusicPlayer.sharedInstance.stopTwoSound()
MusicPlayer.sharedInstance.stopThreeSound()
println("four")
}
}