聲音停止的原因是因爲你只需要一個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我曾經嘗試過你的課,如果你想同時開始兩種聲音,但是我希望它能夠在第二個聲音開始播放的時候,我不希望它突然停止聲音已經在玩。感謝您的幫助 –
我不確定我是否理解問題。 –