Search code examples
swiftswiftuiaudioaudio-recordingrecording

Is there anyway in Swift(UI) to be able to record sound played through the device while ignoring the microphone


I'm writing an app that plays random sounds directly through it. The user needs to be able to record these sounds without any input from the microphone (though there will be the option to include input from the microphone as well, but that part is easy!)

I can't seem to figure out any way of just recording sounds coming through the device, excluding microphone input.

Can anyone point me in the right direction for this? Currently using AVAudioRecord / AVAudioSession, but happy to switch to something else if that makes it easier / possible!

I tried looking through the various settings, both in code and in the documentation, but I am struggling to find anything that actually refers to this at all.

Thanks


Solution

  • You'll want to use AVAudioEngine for this. This is a bit lower-level interface to the audio system that lets you access the data. This will replace any AVAudioRecorder or AVAudioPlayer code you currently have.

    Look at AVAudioSinkNode or AVAudioNode.installTap(onBus:...). These are both designed for different versions of your use case. My guess is you'll want the second one. If you search for installTap, you should find a lot of examples (it's a fairly popular tool).

    What you're likely going to want is an extra AVAudioMixerNode that includes everything without the system input. You can tap that and then join it with the system input at the main mixer node. This is exactly the kind of thing mixers are for. (Your actual problems may be simpler than I'm picturing, so don't feel you need to add an extra mixer if you don't need it.)