How to Play multiple audio files simultaneously

时光怂恿深爱的人放手 提交于 2019-11-26 21:56:23

问题


how to Play numbers Of audio files at same time With AVAudioPlayer? Is it Possible to Play numbers Of audio files can play at same time using AVAudioPlayer? or any other way to play numbers of audio files at same time ? Thank You!


回答1:


Files with below formats can be played simultaneously on iPhone.

AAC, MP3, and ALAC (Apple Lossless) audio: have CPU resource concern. Linear PCM and IMA/ADPCM (IMA4 audio): without CPU resource concerns.

You just need to create a new player instance for every music file, that you want to play.

Sample Code snippet:

-(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];
}

Convert to supported format (i.e. mp3 to caf):

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

Detailed tutorial:

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




回答2:


Swift 2+ Solution

-> Now you can play multiple sounds simultaneously with mp3 or m4a suffix. However, if you want to convert mp3 to m4a you can download this free program from official website: http://audacityteam.org/download/mac/

Also you may need FFmpeg 2+ to convert process: http://lame.buanzo.org/#lameosxdl

-> I prefer to use m4a because, in short it is Apple's own audio format.

//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.



来源:https://stackoverflow.com/questions/28299679/how-to-play-multiple-audio-files-simultaneously

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!