Mic not working properly- Audio device number changing between sessions

If this template helps then use it. If not then just delete and start from scratch.

OS: windows 10
PsychoPy version : v2023.1.2
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:

I have a routine with a mic component to record and save wav files. I am using the Builder, with some code components. I initially just put a mic component and it worked when set the library to ptb and the audio latency priority to 1 (:share low-latency driver). I want the mic to record participant responses but it is not working reliably. Sometimes it works fine, other times it crashes mid recording and says “Alert 4330:Recording device ‘None’ not found, using default device instead”. In my mic component, I have already selected default as the device.

If I chose the microphone of my computer as device (= Microphone Realtek) on the builder, it usually returns this kind of error: “Alert 4330:Recording device ‘5’ not found, using default device instead.”. I have read online that this is because psychopy doesn’t use the same device indexing than my laptop (still quite confusing to me).

What did you try to make it work?:
Initially, I was not using any code component or anything but just wanted to use the mic in the builder. But upon crashing repeatedly, I added this line of code to get the device index:

from psychopy.hardware import keyboard
devices = sound.microphone.Microphone.getDevices()
devicesByIndex = {d.deviceIndex: d for d in devices}
print(devicesByIndex)

Which then returns something like:

So then I just add the code below in a component at “begin routine” of the mic routine, where I manually write the device number obtained from the code above :

    device=7, channels=4, 
    sampleRateHz=48000, maxRecordingSize=24000.0
)

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

It just keeps crashing and telling me:

Alert 4330:Recording device ‘None’ not found, using default device instead.

The thing is that the mic in the builder itself is already set to default, but it doesn’t work. In addition, when I use the code to set it to the audio device index returned by Psychopy, it still resets to “None”. In the Python script generated from the builder, I see this line of code:

# create a microphone object for device: default
defaultMicrophone = sound.microphone.Microphone(
    device=None, channels=None, 
    sampleRateHz=48000, maxRecordingSize=24000.0
)

I assume this is the code that adds the mic component, but why does it select “None” for device, when I have manually selected “default” on the mic component?
I am puzzled, especially because of the inconsistency of it working or not. Sometimes it works fine, sometimes I doesn’t at all.
Could anyone give me their opinion? Any advice would be highly appreciated. Please let me know if I can add some more information.