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