声音停止的原因是因为你只需要一个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)
将发挥SOUND2 Sound1例子后一秒钟,然后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
}
}
你有没有给我班上课? –
@OlivierWilkinson我曾经尝试过你的课,如果你想同时开始两种声音,但是我希望它能够在第二个声音开始播放的时候,我不希望它突然停止声音已经在玩。感谢您的帮助 –
我不确定我是否理解问题。 –