Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 7417

Python • Rasberry Pi4 vlc audio out

$
0
0
I bought a 7" touch screen with 2 speakers. I was having trouble getting the audio working as HDMI output with my python code.

Code:

#!/usr/bin/env pythonimport osimport vlcimport RPi.GPIO as GPIOimport logginglogging.basicConfig(level=logging.INFO)log = logging.getLogger(__file__)# Setup the raspberry pi pinsBUTTON_PIN_1 = 40BUTTON_PIN_2 = 38BUTTON_PIN_3 = 36BUTTON_PIN_4 = 32HOME = os.path.dirname(os.path.realpath(__file__))print(HOME)GPIO.setmode(GPIO.BOARD)GPIO.setup(BUTTON_PIN_1, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(BUTTON_PIN_2, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(BUTTON_PIN_3, GPIO.IN, pull_up_down=GPIO.PUD_UP)GPIO.setup(BUTTON_PIN_4, GPIO.IN, pull_up_down=GPIO.PUD_UP)def play_video(player, media):    """    Play the selected video    :param player: vlc player instance    :param media: video to play    :return:    """    # You need to call "set_media()" to (re)load a video before playing it    player.set_media(media)    player.play()def main():    """    Main loop to play each video    :return: Never    """    # Set up a player instance. Suppress lots of log messages    instance = vlc.Instance("--verbose=-1")    player = instance.media_player_new()    # Create libVLC objects representing each video    video1 = vlc.Media(os.path.join(HOME,"videos/big_buck_bunny_480p_10mb.mp4"))    video2 = vlc.Media(os.path.join(HOME,"videos/Caminandes_Trailer-1080p.mp4"))    video3 = vlc.Media(os.path.join(HOME,"videos/The_End.mp4"))    video4 = vlc.Media(os.path.join(HOME,"videos/andre_wally.mp4"))    # Start the player for the first time    play_video(player, video1)    current_video = video1    # TODO: Add some error handling or at least a proper Ctrl-C handler    while True:        # See which button was pressed and the right video        if not GPIO.input(BUTTON_PIN_1):            if not current_video == video1:                current_video = video1                play_video(player, current_video)        elif not GPIO.input(BUTTON_PIN_2):            if not current_video == video2:                current_video = video2                play_video(player, current_video)        elif not GPIO.input(BUTTON_PIN_3):            if not current_video == video3:                current_video = video3                play_video(player, current_video)        elif not GPIO.input(BUTTON_PIN_4):            if not current_video == video4:                current_video = video4                play_video(player, current_video)        # Loop video if playback is ended        if player.get_state() == vlc.State.Ended:            play_video(player, current_video)if __name__ == '__main__':    main()
I was able to get the code to work by adding this overlay dtoverlay=vc4-kms-v3d, but it only works when I type in the python command python3 play.py If I run it from rc.local or as a systemd service, it runs but I don't get audio. Anyone have any idea whats happening?

Statistics: Posted by CrabbyPete — Mon Jan 27, 2025 4:11 pm — Replies 1 — Views 34



Viewing all articles
Browse latest Browse all 7417

Trending Articles