3

The following code works on my iPhone 5, meaning it correctly "reads" the microphone.

let input = audioEngine.inputNode
let mixer = AVAudioMixerNode()

audioEngine.attach(mixer)
let format = AVAudioFormat(commonFormat: AVAudioCommonFormat.pcmFormatFloat32, sampleRate: SAMPLE_RATE, channels: 1, interleaved: false)
audioEngine.connect(input, to: mixer, format: input.inputFormat(forBus: 0))

mixer.installTap(onBus: 0, bufferSize: 2048, format: format) {
            (buffer: AVAudioPCMBuffer!, time: AVAudioTime!) -> Void in ... }

Unfortunately, it only outputs 0.0 samples when using the app in the simulator. The microphone works generally (tested Siri in the simulator), but in my app does not get the samples correctly.

Is it somehow possible to read samples from the microphone in the simulator at a desired sample rate? I need to send the samples @ 16khz to a server, so there is no option to sample at 44100hz.

CodeBender
  • 35,668
  • 12
  • 125
  • 132
Simon Hessner
  • 1,757
  • 1
  • 22
  • 49
  • no https://stackoverflow.com/questions/20090128/is-it-possible-to-record-actual-sound-on-the-simulator-using-mic – Lu_ Mar 19 '18 at 20:18
  • WTF. This is so absolutely counter-intuitive and furthermore poorly documented... Thank you! – Simon Hessner Mar 19 '18 at 20:50
  • The reason why I need to simulate the microphone stuff is that my app transmits the recorded samples to a server and I need to test different connectivity problems (connection loss, packet loss, slow connection, etc.) I think the simulator would be the best way to do this... Can I simulate different connection situations in xCode while using real hardware? (iPhone) – Simon Hessner Mar 19 '18 at 20:52
  • Settings > Developer > Network Link Conditioner – Lu_ Mar 20 '18 at 11:10
  • Hi, yes this works, but the problem is that I have to put my app in background mode in order to change these settings. This is different to the real-world scenario where the app is running in foreground-mode and the connection is lost. – Simon Hessner Mar 20 '18 at 16:51

0 Answers0