2016-04-26 41 views
7

我试图让多个声音文件在AVAudioPlayer实例上播放,但是当一个声音播放时,另一个声音会停止。我一次只能播放一个以上的声音。这里是我的代码:获取AVAudioPlayer一次播放多个声音

import AVFoundation 

class GSAudio{ 

    static var instance: GSAudio! 

    var soundFileNameURL: NSURL = NSURL() 
    var soundFileName = "" 
    var soundPlay = AVAudioPlayer() 

    func playSound (soundFile: String){ 

     GSAudio.instance = self 

     soundFileName = soundFile 
     soundFileNameURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(soundFileName, ofType: "aif", inDirectory:"Sounds")!) 
     do{ 
      try soundPlay = AVAudioPlayer(contentsOfURL: soundFileNameURL) 
     } catch { 
      print("Could not play sound file!") 
     } 

     soundPlay.prepareToPlay() 
     soundPlay.play() 
    } 
} 

任何人都可以请告诉我如何获取多个声音文件一次玩吗?任何帮助深表感谢。

非常感谢, 凯

+0

你有没有给我班上课? –

+0

@OlivierWilkinson我曾经尝试过你的课,如果你想同时开始两种声音,但是我希望它能够在第二个声音开始播放的时候,我不希望它突然停止声音已经在玩。感谢您的帮助 –

+0

我不确定我是否理解问题。 –

回答

10

声音停止的原因是因为你只需要一个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 
    } 

} 
+0

感谢您的编辑,但是当我尝试使用这个类时,我得到:命令失败,由于信号:分割故障:11.你知道如何解决这个问题吗?非常感谢 –

+0

是的,我刚看到我自己,我无意中复制了一个与另一个冲突的函数。两秒 –

+0

我编辑了代码以避免分段错误,参见上文。或者只是删除playSounds(soundFileNames:String ...,withDelay:Double)函数,使用数组的那个函数就可以了:) –

1

我创建了一个助手库简化了在Swift中播放声音的过程。它会创建多个AVAudioPlayer实例,以允许多次同时播放相同的声音。你可以从Github上下载它或者用Cocoapods导入。

这里是链接:SwiftySound

的用法很简单,因为它可以:

Sound.play(file: "sound.mp3") 
+0

你是英雄我的朋友 –

1

所有的答案张贴的代码页;它不需要那么复杂。

// Create a new player for the sound; it doesn't matter which sound file this is 
       let soundPlayer = try AVAudioPlayer(contentsOf: url) 
       soundPlayer.numberOfLoops = 0 
       soundPlayer.volume = 1 
       soundPlayer.play() 
       soundPlayers.append(soundPlayer) 

// In an timer based loop or other callback such as display link, prune out players that are done, thus deallocating them 
     checkSfx: for player in soundPlayers { 
      if player.isPlaying { continue } else { 
       if let index = soundPlayers.index(of: player) { 
        soundPlayers.remove(at: index) 
        break checkSfx 
       } 
      } 
     }