I use PyAudio with a USB microphone for recording.The code is shown below.
import pyaudio import wave CHUNK = 3200 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 16000 RECORD_SECONDS = 4 def rec(file_name): p = pyaudio.PyAudio() print(p.get_device_count()) stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("开始录音,请说话......") frames =  for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) print(data) frames.append(data) print("录音结束,请闭嘴!") stream.stop_stream() stream.close() p.terminate() wf = wave.open(file_name, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() rec("2020-0623.wav")
When NX is started first, the microphone is inserted later,after the above code runs,I get 2020-0623.wav.And it has sound.
But when nx restart with microphone,run the code again,I can still get ‘2020-0623.wav’.But it has no sound.The data read from stream is ‘0x000x000x000x00……’。
You see,my USB microphone device id is not specify in the above codes.But when the NX starts first and the microphone is inserted later,the SUB microphone was actually used during the recording.And the frame rate of my USB microphone is 48K,in the code above it set as 16K,but it works.
My microphone has a built-in sound card，it seems that，when nx restart with microphone PyAudio dosen’t use my USB microphone, it use a default audio card.
So,i want to know how to match the default sound card and my USB microphone,to excute the code normal after nx reboots.