音频停止的原因是因为您只设置了一个 AVAudioPlayer,因此当您要求类播放另一种声音时,您当前正在用新的 AVAudioPlayer 实例替换旧实例。您基本上是在覆盖它。
您可以创建 GSAudio 类的两个实例,然后在每个实例上调用 playSound,或者使该类成为使用 audioPlayers 字典的通用音频管理器。
我更喜欢后一种选择,因为它允许更简洁的代码并且效率更高。您可以检查您之前是否已经为声音制作了播放器,而不是例如制作新播放器。
无论如何,我为您重新制作了您的课程,以便它可以同时播放多种声音。它也可以在自身上播放相同的声音(它不会替换之前的声音实例)希望对您有所帮助!
这个类是一个单例,所以要访问这个类使用:
GSAudio.sharedInstance
例如,播放您会调用的声音:
GSAudio.sharedInstance.playSound("AudioFileName")
同时播放多个声音:
GSAudio.sharedInstance.playSounds("AudioFileName1", "AudioFileName2")
或者您可以将声音加载到某个数组中,然后调用接受数组的 playSounds 函数:
let sounds = ["AudioFileName1", "AudioFileName2"]
GSAudio.sharedInstance.playSounds(sounds)
我还添加了一个 playSounds 函数,允许您延迟以级联格式播放的每个声音。所以:
let soundFileNames = ["SoundFileName1", "SoundFileName2", "SoundFileName3"]
GSAudio.sharedInstance.playSounds(soundFileNames, withDelay: 1.0)
会在 sound1 之后播放 sound2,然后 sound3 会在 sound2 之后播放一秒,以此类推。
这是课程:
class GSAudio: NSObject, AVAudioPlayerDelegate {
static let sharedInstance = GSAudio()
private override init() {}
var players = [NSURL:AVAudioPlayer]()
var duplicatePlayers = [AVAudioPlayer]()
func playSound (soundFileName: String){
let soundFileNameURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundFileName, ofType: "aif", inDirectory:"Sounds")!)
if let player = players[soundFileNameURL] { //player for sound has been found
if player.playing == false { //player is not in use, so use that one
player.prepareToPlay()
player.play()
} else { // player is in use, create a new, duplicate, player and use that instead
let duplicatePlayer = try! AVAudioPlayer(contentsOfURL: soundFileNameURL)
//use 'try!' because we know the URL worked before.
duplicatePlayer.delegate = self
//assign delegate for duplicatePlayer so delegate can remove the duplicate once it's stopped playing
duplicatePlayers.append(duplicatePlayer)
//add duplicate to array so it doesn't get removed from memory before finishing
duplicatePlayer.prepareToPlay()
duplicatePlayer.play()
}
} else { //player has not been found, create a new player with the URL if possible
do{
let player = try AVAudioPlayer(contentsOfURL: soundFileNameURL)
players[soundFileNameURL] = player
player.prepareToPlay()
player.play()
} catch {
print("Could not play sound file!")
}
}
}
func playSounds(soundFileNames: [String]){
for soundFileName in soundFileNames {
playSound(soundFileName)
}
}
func playSounds(soundFileNames: String...){
for soundFileName in soundFileNames {
playSound(soundFileName)
}
}
func playSounds(soundFileNames: [String], withDelay: Double) { //withDelay is in seconds
for (index, soundFileName) in soundFileNames.enumerate() {
let delay = withDelay*Double(index)
let _ = NSTimer.scheduledTimerWithTimeInterval(delay, target: self, selector: #selector(playSoundNotification(_:)), userInfo: ["fileName":soundFileName], repeats: false)
}
}
func playSoundNotification(notification: NSNotification) {
if let soundFileName = notification.userInfo?["fileName"] as? String {
playSound(soundFileName)
}
}
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
duplicatePlayers.removeAtIndex(duplicatePlayers.indexOf(player)!)
//Remove the duplicate player once it is done
}
}