6
votes

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.

Regarding frameworks it seems clear from what I've looked at so far that AVFoundation 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 AVAudioPlayer. I am planning to start prototyping something with this soon.

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.

If no answer is forthcoming I will post my own code when I get there.

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.

Research Notes and Resources

Apple: AVFoundation

A collection of resources relating to AVFoundation.

Apple: AVFoundation Programming Guide

This document seems encouraging at first, but actually only deals with video. It says:

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.

The "Multimedia Programming Guide" 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.

Rudi Strahl: Mixing Multiple Audio Tracks with AVFoundation

Compares using AVComposition to using multiple AVPlayers. Example code is Objective-C. Not sure how the AVPlayers are mixed in the second solution. Perhaps with AVAudioMix. Currently looking at this. The article talks a little about it but doesn't deliver any specifics.

Audio Session Programming Guide

This document looks at AVAudioSession which provides supporting functionality:

AVAudioSession gives you control your app’s audio behavior. You can:

  • Select the appropriate input and output routes for your app
  • Determine how your app integrates audio from other apps
  • Handle interruptions from other apps
  • Automatically configure audio for the type of app your are creating

Techotopia: Playing Audio on iOS 10 using AVAudioPlayer

Some useful information on using AVAudioPlayer.

Stack Overflow: Playing a Sound with AVAudioPlayer

Basic Swift code for playing a sound. Some answers include a little extra functionality.

Hacking with Swift: How to Play Sounds Using AVAudioPlayer

Again, covers the basics.

Sweet Tutos: How To Play Sounds Files And Manage Duration Progress – AVAudioPlayer Tutorial

Updated to Swift 3. Some useful info.

Xamarin: Playing Sound with AVAudioPlayer

Written in Swift 2, I think.

Apple Video: WWDC 2013 Moving to AV Kit and AV Foundation

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.

1
Did you ever figure out a path or a solution for this? I'm running up against the same issue and would love any tips or tricks you could provide.NathanD
Yes, I did. From what I remember AVAudioPlayer will do everything you need.user2493235
Yes, was definitely AVAudioPlayer as I have another question on it here. AVFoundation is more low level and wasn't necessary.user2493235
So you had an audioPlayer for each mp3 and you just used play(at: ) for each song with a specified delay so that they would be in sync?NathanD
@SuperDuperApps I am facing the same issue you did. Could you please help? stackoverflow.com/questions/54345702bibscy

1 Answers

0
votes

I was working on the same problem, best what I could do it is, to transcode media content to be playing using avplayer, here is a draft, maybe it can help.