简体   繁体   中英

iOS Multi-Channel Audio with AVFoundation and Swift

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

我正在解决同样的问题,我能做的最好的是,将媒体内容转码为使用 avplayer 播放,这是一个草稿,也许它可以提供帮助。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM