Hi all, i have installed pi os and have been able to play a song from the usb drive with vlc with output from the dac and it sounds great, i wrote a script to play a song from usb and that works great. I then wrote another script to be able to use gpio and call songs like a jukebox, folder 01 track 01 with the ht16k33 displaying the key presses, that works great but i do not get any audio and the debugging shows the song playing, my issue is then when i run the other script i dont get any sound or through vlc. If reboot the sound is fine again. Its only when i run the script with the button inputs no sound. Will post scripts a little later cheers
Hi Paul,
It’d be great if you could include those scripts. Its a funny issue so I think the answer will lay there.
here is the script that plays the usb drive fine.
audiotest.py
import vlc
import time
# Update the path to your USB drive and the specific folder and file
# Example: /media/cd50/MUSIC/01/your_track.mp3
# Replace 'your_track.mp3' with the actual name of the music file you want to play
music_file_path = '/media/cd50/MUSIC/01/01.mp3'
# Create a VLC instance
player = vlc.Instance()
# Create a new player
media_player = player.media_player_new()
# Load the media file
media = player.media_new(music_file_path)
# Set the media player media
media_player.set_media(media)
# Play the media
media_player.play()
# Let it play for the duration of the track or until stopped
# You might want to implement a better way to handle playback duration
time.sleep(300) # Adjust or remove based on your requirement
# Stop playing after the duration
media_player.stop()
Here is my script using gpio buttons and display
import RPi.GPIO as GPIO
import time
import board
import busio
import vlc
from adafruit_ht16k33 import segments
# Set up logging for VLC
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Setup for the 14-segment display
i2c = busio.I2C(board.SCL, board.SDA)
display = segments.Seg14x4(i2c)
display.fill(0) # Clear the display
# Create a VLC instance
player = vlc.Instance()
media_player = player.media_player_new()
# GPIO setup
GPIO.setmode(GPIO.BCM)
button_pins = [4, 17, 27, 26, 10, 9, 11, 5, 6, 13] # Button pins
reset_pin = 19 # Reset button pin
last_four_digits = [] # Holds the last four digits entered
def play_music(folder, track):
music_base_path = '/media/cd50/MUSIC' # Corrected USB drive mount point
file_path = f"{music_base_path}/{folder}/{track}.mp3" # Adjust extension if necessary
try:
media = player.media_new(file_path)
media_player.set_media(media)
media_player.play()
logging.debug(f"Playing: {file_path}")
except Exception as e:
logging.error(f"Error playing {file_path}: {e}")
def update_display():
display.fill(0) # Clear the display
display.print(''.join(last_four_digits)) # Show the last four digits
def button_callback(channel):
global last_four_digits
if channel == reset_pin:
last_four_digits = [] # Clear the digits
else:
# Map the channel to the corresponding digit
digit = str(button_pins.index(channel) if channel != button_pins[0] else 0)
last_four_digits.append(digit)
# Check if a complete selection is made
if len(last_four_digits) == 4:
folder = ''.join(last_four_digits[:2])
track = ''.join(last_four_digits[2:])
play_music(folder, track) # Play the selected music
last_four_digits = [] # Reset the input after playing a track
update_display()
# Setup buttons
for pin in button_pins + [reset_pin]:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.FALLING, callback=button_callback, bouncetime=300)
try:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
media_player.stop() # Stop music playback
display.fill(0)
GPIO.cleanup()
Regards Paul
not sure why the font has gone weird, ok clicked preformatted text which fixed it
Hi @paul139837 - if i had to guess it might be problematic playing music from inside an interrupt callback.
Perhaps it is better to set a state flag that is polled in the main loop → So media_player.play()
is only ever called during the main loop.
Now the main loop might look something like this:
folder = None
track = None
selection_made = False
def button_callback(channel):
global last_four_digits, folder, track
if channel == reset_pin:
last_four_digits = [] # Clear the digits
else:
# Map the channel to the corresponding digit
digit = str(button_pins.index(channel) if channel != button_pins[0] else 0)
last_four_digits.append(digit)
# Check if a complete selection is made
if len(last_four_digits) == 4:
folder = ''.join(last_four_digits[:2])
track = ''.join(last_four_digits[2:])
selection_made = True
last_four_digits = [] # Reset the input after playing a track
update_display()
while True:
if selection_made == True:
selection_made = False
play_music(folder, track) # Play the selected music
time.sleep(0.1)
This code isn’t demonstrated to be functional, it’s more to communicate the idea of using flags that pass information out of the callback.
Does that make sense?
I will take a look at that idea and see what i can come up with, thanks for your response. This now gives me something to move forward on this project
cheers Paul.
`So i have made some changes to my script as you suggested and it has helped with the response of the button inputs upon boot, I think the issue lies with the alsa and pulse audio settings - there isn’t any /etc/asound.conf file and when I aplay -l find the dac, I think it streams via pulseaudio - pactl list short sinks should list dac but it is not showing - not sure i could be wrong.
It looks like the fault is with the code, when the script accumulates button presses, decodes them into folder and track, and plays the corresponding song: it works, thanks for your help.
After further investigation - I first got buttons working and then display working which uses i2c, I did not get any sound. I then reconfigured and now I have buttons working and sound working but no display - maybe a conflict with dac and display so i might try a 4 digit display that does not use i2c unless anyone has some info, I have also tried a tm1650 display with no luck.
Hi Paul,
Glad to hear you got the split tests working!
Would it be possible to send through some links of the products you are using?
PS: a great reference is https://pinout.xyz/ for all things Pi!
Liam
HI Paul. I see you have managed to somehow install the adafruit_HT16K33 code on a raspberry pi. How did you do that please? I installed the CircuitPython version and it just gets errors.
Thank you