【发布时间】:2018-05-06 08:17:18
【问题描述】:
我正在通过子进程使用 arecord 在树莓派中录制:按下操纵杆按钮开始录制并再次按下按钮停止录制:我使用 p.terminate() 停止录制,这是我的代码。我不知道这是否是终止子进程的正确方法?
提前致谢。
if joystick.get_numbuttons() >= 1 and joystick.get_button( 0 ) == 0 and button_pressed:
button_pressed = False
is_recording = not is_recording
print(is_recording)
#start recording
if is_recording:
dotting = dotting + 1
#recording code
datetime.now().strftime("%Y%m%d_%H%M%S")
filename = "%s.wav" % datetime.now().strftime("%Y%m%d_%H%M%S")
p=subprocess.Popen(['arecord', '--device=hw:1,0', '--format', 'S16_LE', '--rate', '44100-c1', filename], shell=False)
#change background
background = pygame.image.load(background_image2).convert()
#start timer
stopFlag = Event()
timer = MyTimer(stopFlag,time.clock())
#timer.start()
text2 = font.render('.', True, BLACK)
screen.blit(background, (0,0))
stopFlag.set()
#record, remaining here
#stop recording
else:
#stop recording, save record
timer.do_run = False
backToPreviousQuestion()
background = pygame.image.load(background_image).convert()
p.terminate()
pygame.display.update()
【问题讨论】:
-
Popen.terminate()是,总的来说,正确的事情。顺便说一句,为了请求对工作代码的反馈(与关于特定问题的问题相反,代码内置在 minimal reproducible example 中以隔离单个问题),请考虑 Code Review 而不是 StackOverflow(尽管 do 在发布之前阅读那里的帮助中心)。 -
谢谢,我是编程菜鸟。我会转到代码审查来询问。
标签: python raspberry-pi subprocess record terminate