iOS Recording from Microphone in Unity

I’m trying to record audio from the microphone on iOS. However, system.recordStart(…) is returning ERR_RECORD when I run on a device (specifically, an iPad 3). It works when running in play mode in Unity, but not on the device. I’ve included my simplified code below.

All this code does is create an FMOD system, gets some driver information, then tries to record 1 second of looping into a sound. I properly get the single device’s capabilities (Rate = 48000, Channels = 1), but the recordStart(…) call fails.

I’ve disabled Unity’s audio, since I’m using the FMOD low level integration myself. Enabling Unity’s audio makes the call work, but I get no audio playback.

Thanks for any help. I’ve been banging my head against this for too long!


using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Text;

public class RecordTest : MonoBehaviour 
  private FMOD.System mSystem = null;

  private int mDeviceIndex = 0;
  private int mRate = 0;
  private int mChannels = 0;

  private uint mByteCount = 0;
  private byte[] mBytes;
  private FMOD.Sound mSound = null;
  private const float kRecordSeconds = 1.0f;
  void Start () 
    FmodCheck( FMOD.Factory.System_Create( out mSystem ) );

    FmodCheck( mSystem.init( 128, FMOD.INITFLAGS.NORMAL, (System.IntPtr)null ) );

    int numDrivers, numConnected;
    FmodCheck( mSystem.getRecordNumDrivers( out numDrivers, out numConnected ) );

    Guid guid;
    StringBuilder nameBuilder = new StringBuilder();
    FMOD.SPEAKERMODE speakerMode;
    FMOD.DRIVER_STATE driverState;
    FmodCheck( mSystem.getRecordDriverInfo( mDeviceIndex, nameBuilder, 0,
                                            out guid, out mRate, out speakerMode,
                                            out mChannels, out driverState ) );
    Debug.Log( "Drivers = " + numDrivers + ", Connected = " + numConnected );
    Debug.Log( "Rate = " + mRate + ", Channels = " + mChannels );

    // 2.0f for the byte size of PCM16.
    mByteCount = (uint)( kRecordSeconds*2.0f*(float)( mChannels*mRate ) );
    mBytes = new byte[mByteCount];
    info.cbsize = Marshal.SizeOf( info );
    info.numchannels = mChannels;
    info.defaultfrequency = mRate;
    info.format = FMOD.SOUND_FORMAT.PCM16;
    info.length = mByteCount;

    FmodCheck( mSystem.createSound( mBytes, FMOD.MODE.OPENMEMORY_POINT | FMOD.MODE.OPENRAW, ref info, out mSound ) );

    const bool kLoop = true;
    FmodCheck( mSystem.recordStart( mDeviceIndex, mSound, kLoop ) );

  private void FmodCheck( FMOD.RESULT iResult )
    if ( iResult != FMOD.RESULT.OK )
      Debug.Log( "FMOD Error = " + iResult );

1 Like

Looks like I just needed to set the audio session category to be AVAudioSessionCategoryPlayAndRecord. Easy!

I’m facing the same problem.
Can you please tell me how to set the audio session category to :AVAudioSessionCategoryPlayAndRecord

Please see the ‘recording’ section of our iOS basics documentation:

I meet same problem, but I can’t to get you give the help link(page not found),so how to get it?

You can find the Recording section in the new link below:

1 Like