3
\$\begingroup\$

I am using arecord through subprocess to record in raspberry pi : press joystick button to start recording and press button again to stop recording. I am really a noob in programming.

while True:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
    joystick_count = pygame.joystick.get_count()

    if(button_pressed == False and joystick.get_button( 0 ) == 1):
            button_pressed = True
            button_released = False

        if not is_recording:
        #detect joystick up        
        if joystick.get_numaxes() >= 1 and joystick.get_axis(1) < 0:    
            time.sleep(0.4)
            current_index = current_index + 1
            screen.blit(background, (0,0))
            screen.blit(images[current_index%len(images)], position1)        
            pygame.display.update()
            print('JOYSTICK DOWN')

        #detect joystick down
        if joystick.get_numaxes() >= 1 and joystick.get_axis(1) > 0:    
            time.sleep(0.4)
            current_index = current_index - 1
            screen.blit(background, (0,0))
            screen.blit(images[current_index%len(images)], position1)        
            pygame.display.update()
            print('JOYSTICK UP')


    #detect button press
    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
            filepath = "%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', filepath], 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

            #back to previous question
            timer.do_run = False
            backToPreviousQuestion()
            background = pygame.image.load(background_image).convert()
            p.terminate()
    pygame.display.update()
\$\endgroup\$
  • \$\begingroup\$ really appreciate for your help. thanks again. \$\endgroup\$ – Cun Li Nov 23 '17 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.