如何同时播放多个音频文件

How to Play multiple audio files simultaneously

如何用AVAudioPlayer同时播放多个音频文件? 是否可以使用 AVAudioPlayer 同时播放多个音频文件? 或任何其他方式同时播放多个音频文件? 谢谢!

以下格式的文件可以在 iPhone.

上同时播放

AAC、MP3 和 ALAC(Apple 无损)音频:有 CPU 资源问题。 线性 PCM 和 IMA/ADPCM(IMA4 音频):没有 CPU 资源问题。

您只需为每个要播放的音乐文件创建一个新的播放器实例。

示例代码片段:

-(void)playSounds{
    [self playSound1];
    [self playSound2];
}

-(void)playSound1{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"file1" 
                                                      ofType:@"m4a"];  
    AVAudioPlayer* player= [[AVAudioPlayer alloc] initWithContentsOfURL:
                                                 [NSURL fileURLWithPath:path]
                                                                  error:NULL];  
    player.delegate = self;  
    [player play];
}

-(void)playSound2{
     SString *path = [[NSBundle mainBundle] pathForResource:@"file2" 
                                                      ofType:@"m4a"];  
    AVAudioPlayer* player= [[AVAudioPlayer alloc] initWithContentsOfURL:
                                                 [NSURL fileURLWithPath:path]
                                                                  error:NULL];  
    player.delegate = self;  
    [player play];
}

转换为支持的格式(即 mp3 到 caf):

/usr/bin/afconvert -f caff -d ima4 sound.mp3 sound.caf

详细教程:

https://brainwashinc.wordpress.com/2009/08/14/iphone-playing-2-sounds-at-once/

Swift 2+

-> 现在您可以同时 播放多个带有 mp3m4a 后缀的声音。但是,如果您想将 mp3 转换为 m4a,您可以从官方网站下载这个免费程序: http://audacityteam.org/download/mac/

您可能还需要 FFmpeg 2+ 来转换过程: http://lame.buanzo.org/#lameosxdl

->我更喜欢用m4a因为,总之它是Apple自己的音频格式。

//import AVFoundation

  // you have to define them first
  var player1 = AVAudioPlayer()
  var player2 = AVAudioPlayer()

  func playMultipleSound() {
    playSound1()
    playSound2()
  }

  func playSound1() {
    let soundUrl = NSBundle.mainBundle().URLForResource("sound1", withExtension: "mp3")! // or m4a

    do {
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
      try AVAudioSession.sharedInstance().setActive(true)

      player1 = try AVAudioPlayer(contentsOfURL: soundUrl)
      player1.numberOfLoops = 0
      player1.prepareToPlay()
      player1.play()
    } catch _ {
      return print("sound file not found")
    }
  }

  func playSound2() {
    let soundUrl = NSBundle.mainBundle().URLForResource("sound2", withExtension: "m4a")! // or mp3

    do {
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
      try AVAudioSession.sharedInstance().setActive(true)

      player2 = try AVAudioPlayer(contentsOfURL: soundUrl)
      player2.numberOfLoops = 0
      player2.prepareToPlay()
      player2.play()
    } catch _ {
      return print("sound file not found")
    }
  }

The only official filename extension for MPEG-4 Part 14 files is .mp4, but many have other extensions, most commonly .m4a and .m4p. M4A (audio only) is often compressed using AAC encoding (lossy), but can also be in Apple Lossless format.