title: iOS Multi-Channel Audio with AVFoundation and Swift
body: <p>I am currently in the research and prototyping stages of a project to develop a native iOS app (Swift 3) that includes a multi-channel audio player (multiple stereo MP3 files). I have found very limited information online, particularly written in Swift 3, so thought as I continue my research I would pose a question here.</p>
  <p>Regarding frameworks it seems clear from what I've looked at so far that <a href="https://developer.apple.com/reference/avfoundation" rel="nofollow noreferrer">AVFoundation</a> is going to do the job. It's not too low level and has a good set of functionality. It has support for playing multiple audio files with <a href="https://developer.apple.com/reference/avfoundation/avaudioplayer" rel="nofollow noreferrer">AVAudioPlayer</a>. I am planning to start prototyping something with this soon.</p>
  <p>But I am new to Swift and to iOS development with its huge number of libraries, so I'm wondering if I'm missing anything, if I'm on the right track here. Any answers with general information and thoughts on this will be up-voted. For an accepted answer some sample outline code using an appropriate framework, AVFoundation or a justified alternative.</p>
  <p>If no answer is forthcoming I will post my own code when I get there.</p>
  <p>Specifically I need from two to ten input channels, from MP3 files within the project resources, each with their own gain that can be individually adjusted, and then all of these mixed, maintaining their stereo channels, to a single output (the device) with a master gain. Some of the tracks need to loop, others not. The tracks need to be accurately synchronised. This is just for info and outline code would be fine covering the important points.</p>
  <p><strong>Research Notes and Resources</strong></p>
  <p><strong><a href="https://developer.apple.com/av-foundation/" rel="nofollow noreferrer">Apple: AVFoundation</a></strong></p>
  <p>A collection of resources relating to AVFoundation.</p>
  <p><strong><a href="https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/00_Introduction.html#//apple_ref/doc/uid/TP40010188" rel="nofollow noreferrer">Apple: AVFoundation Programming Guide</a></strong></p>
  <p>This document seems encouraging at first, but actually only deals with video. It says:</p>
  <blockquote>
  <p>There are two facets to the AVFoundation framework—APIs related to video and APIs related just to audio. The older audio-related classes provide easy ways to deal with audio. They are described in the Multimedia Programming Guide, not in this document.</p>
  </blockquote>
  <p>The &quot;Multimedia Programming Guide&quot; which is also mentioned elsewhere at Apple in relation to this, is never linked and Google results point to not found pages on the Apple site. It seems to have disappeared.</p>
  <p><strong><a href="http://www.rudistrahl.me/journal/Mixing-Multiple-Audio-Tracks-With-AVFoundation" rel="nofollow noreferrer">Rudi Strahl: Mixing Multiple Audio Tracks with AVFoundation</a></strong></p>
  <p>Compares using <a href="https://developer.apple.com/reference/avfoundation/avcomposition" rel="nofollow noreferrer">AVComposition</a> to using multiple <a href="https://developer.apple.com/reference/avfoundation/avplayer" rel="nofollow noreferrer">AVPlayer</a>s. Example code is Objective-C. Not sure how the AVPlayers are mixed in the second solution. Perhaps with <a href="https://developer.apple.com/reference/avfoundation/avaudiomix" rel="nofollow noreferrer">AVAudioMix</a>. Currently looking at this. The article talks a little about it but doesn't deliver any specifics.</p>
  <p><strong><a href="https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007875" rel="nofollow noreferrer">Audio Session Programming Guide</a></strong></p>
  <p>This document looks at <a href="https://developer.apple.com/reference/avfoundation/avaudiosession" rel="nofollow noreferrer">AVAudioSession</a> which provides supporting functionality:</p>
  <blockquote>
  <p>AVAudioSession gives you control your app’s audio behavior. You can:</p>
  <ul>
  <li>Select the appropriate input and output routes for your app</li>
  <li>Determine how your app integrates audio from other apps</li>
  <li>Handle interruptions from other apps</li>
  <li>Automatically configure audio for the type of app your are creating</li>
  </ul>
  </blockquote>
  <p><strong><a href="http://www.techotopia.com/index.php/Playing_Audio_on_iOS_8_using_AVAudioPlayer#Controlling_and_Monitoring_Playback" rel="nofollow noreferrer">Techotopia: Playing Audio on iOS 10 using AVAudioPlayer</a></strong></p>
  <p>Some useful information on using <a href="https://developer.apple.com/reference/avfoundation/avaudioplayer" rel="nofollow noreferrer">AVAudioPlayer</a>.</p>
  <p><strong><a href="https://stackoverflow.com/questions/24393495/playing-a-sound-with-avaudioplayer">Stack Overflow: Playing a Sound with AVAudioPlayer</a></strong></p>
  <p>Basic Swift code for playing a sound. Some answers include a little extra functionality.</p>
  <p><strong><a href="https://www.hackingwithswift.com/example-code/media/how-to-play-sounds-using-avaudioplayer" rel="nofollow noreferrer">Hacking with Swift: How to Play Sounds Using AVAudioPlayer</a></strong></p>
  <p>Again, covers the basics.</p>
  <p><strong><a href="http://sweettutos.com/2016/02/23/how-to-play-sounds-files-and-manage-duration-progress-avaudioplayer-tutorial/" rel="nofollow noreferrer">Sweet Tutos: How To Play Sounds Files And Manage Duration Progress – AVAudioPlayer Tutorial</a></strong></p>
  <p>Updated to Swift 3. Some useful info.</p>
  <p><strong><a href="https://developer.xamarin.com/recipes/ios/media/sound/avaudioplayer/" rel="nofollow noreferrer">Xamarin: Playing Sound with AVAudioPlayer</a></strong></p>
  <p>Written in Swift 2, I think.</p>
  <p><strong><a href="https://developer.apple.com/videos/play/wwdc2013/606/" rel="nofollow noreferrer">Apple Video: WWDC 2013 Moving to AV Kit and AV Foundation</a></strong></p>
  <p>While not directly related, I found the first 30 minutes of this video introducing developers to AV Kit and AV Foundation in OS X 10 provides a useful overview of the technology.</p>