8

I'm trying to capture audio, using the method in this question; with AVCaptureSession and AVCaptureAudioDataOutput. This seems to work fine with 1 inconvenience: it doesn't work in the simulator. Both AVAudioRecorder, and the good old SpeakHere demo app, work fine in the simulator, using the internal microphone on my MacBook Pro.

Problem is that [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio] gives null in the simulator, so subsequent code fails with the message (when it tries to add null as input to the AVCaptureSession):

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** Can't add <AVCaptureDeviceInput: 0x9138b40 [(null)]> because the device does not support AVCaptureSessionPresetHigh.  Use -[AVCaptureDevice supportsAVCaptureSessionPreset:].'

Is there an easy way to get this to work in the simulator?

Community
  • 1
  • 1
Claude
  • 8,806
  • 4
  • 41
  • 56
  • I do not know why the AVCaptureSession setup is not working, however if you will not necessarily need to test the recording on the simulator but only on the device you can still exclude the recording part on the iOS-Simulator using: #if TARGET_IPHONE_SIMULATOR // Simulator code here #else // "Real" iOS Device code here #endif This will prevent an app crash on the simulator. – Markus Mar 15 '12 at 00:55

1 Answers1

14

The AVCaptureDevice class is not implemented on the simulator (as of this writing at least, maybe it will change in the future).

Try e.g.

NSLog(@"%@", [AVCaptureDevice devices]);

which will produce an empty list on the simulator (but will list all capture devices if compiled and run on a real iDevice).

This kind of functionality needs to be tested on a real device anyway (due to the hardware detail dependency, such as latency and sample formats supported), so for testing purposes it is not that important to have device support in the simulator. But it would indeed be nice to have it for demo purposes.

To have a demo running on the simulator, maybe you can mock the capture input. If you really want to spend some time on it, maybe you can fallback on the AVAudioRecorder for the simulator (I never used AVAudioRecorder, so I can't tell if it does or does not work on the simulator, but from your question I read that it does).

Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • 2
    That's a real shame. I had expected (hoped) that AVAudioRecorder was built on top of AVCaptureSession, so I guess this is not the case. I do agree that in the end I want to test this stuff on a device, but during iterative development cycles it would be great if I could just use the simulator. – Claude Mar 17 '12 at 16:11
  • For that I would recommend recording audio (using a real device, depending on what type of app it is) and #ifdef in static playback of that file when using the simulator. – Krumelur Mar 17 '12 at 16:20
  • This has changed, it no longer outputs an empty array. I'm using Xcode 5.0.2. The iPad iOS 5.0 simulator returns 2 devices: Front camera, Rear camera. – neoneye Dec 13 '13 at 09:08
  • As of Xcode 6.1, with an iOS 8 simulator, `(lldb) po [AVCaptureDevice devices] <__NSArrayM 0x78e73430>( )` which shows that there are no audio capture devices. – Streeter Nov 26 '14 at 17:36