CircuitPython School - Playing Sound (wav or mp3) with PyGame on a Raspberry Pi

  Рет қаралды 20,150

John Gallaugher

John Gallaugher

2 жыл бұрын

For makers: Want to play sound from your projects? Using PyGame in your python code is a great choice! And it integrates perfectly with CircuitPython. We'll work with a Raspberry Pi that has an audio jack, but I'll also discuss how Raspberry Pi Zero owners can add audio out to their boards. We'll learn to test audio on our board, and we'll write a simple program that will loop through a list of files, playing them one at a time. Part of Prof. Gallaugher's Physical Computing course. Find all videos on his KZbin channel. Enjoy & tell others!

Пікірлер: 28
@ajwilliams5551
@ajwilliams5551 2 жыл бұрын
Your content is immaculate. This is the second life-saving video I've watched from you. Learning so much just 2 days into Raspberry Pi!
@profgallaugher
@profgallaugher 2 жыл бұрын
So glad you are enjoying this. Shoot me an address over email if you’d like a “Make Something Awesome!” Sticker.
@richardskidmore6632
@richardskidmore6632 5 ай бұрын
Brilliant - many thanks. I agree, a lifesaver for me. Been trying for days to play wav files on my pi zero w and this is the only code that will do it for some reason. Many, many thanks.👍
@avabanbury594
@avabanbury594 2 жыл бұрын
Thank you so much! This helped me with a project for my Intro to Computer-Science class!!
@profgallaugher
@profgallaugher 2 жыл бұрын
Glad it helped, Ava! Keep hacking!
@MuhammedBasil
@MuhammedBasil 5 ай бұрын
This is helpful. Thank you.
@nayanrimal3206
@nayanrimal3206 Жыл бұрын
good job bro
@richardskidmore6632
@richardskidmore6632 5 ай бұрын
Ps my code is exactly the same but only plays the last in the list. Any idea what I’ve done wrong? Ta. Rik
@profgallaugher
@profgallaugher 2 жыл бұрын
I noticed one time on a fresh install the speaker-test & aplay produced only heavy static for the length of the play, however playing files through python did work. It also worked when logging in via VNC and using VLC player, so if you hear static when testing & running aplay, just continue & hopefully your sound will work fine. If anyone knows why speaker-test & aplay are not working on some installs, do comment, below!
@ccf805
@ccf805 2 жыл бұрын
The speaker-test works fine for me. When I attempt to play a .wav, .mp3 or .m4a audio file, I only hear white noise and get message "Playing raw data 'audiofilename.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono. I'm using a Raspberry Pi 4 with Raspberry Pi OS and using bluetooth for my Google Home Mini.
@snerkrabbledauber7032
@snerkrabbledauber7032 2 жыл бұрын
I had that happen. I realized I was playing an mp3 file. aplay was playing it as a wav.
@profgallaugher
@profgallaugher 2 жыл бұрын
If your Pi repeatedly asks "To install the screen reader press CTRL+ALT+SPACE", you can delete that auto-repeat by entering the following command, then rebooting your Pi: sudo rm /etc/xdg/autostart/piwiz.desktop
@mugtabataha3355
@mugtabataha3355 2 жыл бұрын
Can you please make a video elaborating the use of the sound function with if statement because i have 6 different classes and i want the raspberry to say the name of each one of them when available
@profgallaugher
@profgallaugher 2 жыл бұрын
Check out the DJ board video kzbin.info/www/bejne/Z3ekiqxnndeWfcU
@nicolebendo9079
@nicolebendo9079 2 жыл бұрын
Hi! I'm using a raspberry pico and my jack port is linked to the pins GP18 and GP19, how can I modifiy this programm so that it plays the sound though that port ? Thanks ! :)
@profgallaugher
@profgallaugher 2 жыл бұрын
Hi. If you are using a Pico you won’t use PyGame. Check out the CircuitPython playlist. There are lots of sound tutorials there and you can use CircuitPython with the Pico. See bit.ly/circuitpython-tutorials.
@jalvarez242424
@jalvarez242424 2 жыл бұрын
Any ideas on how to add buttons to each file? Thanks.
@profgallaugher
@profgallaugher 2 жыл бұрын
Check out the video on bit.ly/raspberry-pi-tutorials on “The Right Away to Add Buttons”. You can then trigger a sound with each button press. If you’re new to CircuitPython also check out the CircuitPython School playlist on my channel. It’s where my students start. Cheers!
@vaibhavbatra4613
@vaibhavbatra4613 2 жыл бұрын
What if I want to play the files in random order? Also, not one after the other but after silences in between? What would be the code for that?
@profgallaugher
@profgallaugher 2 жыл бұрын
Check out the playlist: bit.ly/circuitpython-tutorials and look for Lesson 18 - Random Numbers. This will show you how to generate random values. You can put the names of your sounds in a list & get a random element from that list and play that element. For a delay of a random amount, you can generate a random value in a range of the # of seconds (or fractions of a second) you want to wait, and use a time.sleep(random_seconds_value) in between getting & playing random sounds. Hope that helps!
@vaibhavbatra4613
@vaibhavbatra4613 2 жыл бұрын
@@profgallaugher Great. Thank you so much!
@manvirbhangu7755
@manvirbhangu7755 2 жыл бұрын
Is there a reason that my rasp pi produces static sound?
@profgallaugher
@profgallaugher 2 жыл бұрын
No idea. Perhaps someone on the Adafruit Discord channel has some suggestions.
@suhasmahajan-if9ez
@suhasmahajan-if9ez 5 ай бұрын
Sir if i will pulg ear phones will it work?
@profgallaugher
@profgallaugher 5 ай бұрын
Try ‘em out, homie!
@user-EricLin0619
@user-EricLin0619 2 жыл бұрын
Why did terminal tell me " NameError: name 'speaker' is not defined? "
@profgallaugher
@profgallaugher 2 жыл бұрын
"NameError ... is not defined" means you're referring to a variable (in your case one named "speaker") that has not been created. If you look hard at the code you'll see that the variable is named "speaker_volume", not "speaker". So if you refer to "speaker" before it has been created & has anything inside it, your program thinks this is a made-up word like flitzblaubgasdf. When you assign a variable a value you define it, or give it a definition. If I tell you flitzblaubgasdf = "林冠廷 is a code god", then I have just defined that variable and you'll know what flitzblaubgasdf is. Check your code & make sure your "speaker" is called "speaker_volume" in all places. Also: Sometimes this will show up if you refer to a value that should be part of an imported library (not the case here). That's another thing to double check. Good luck!
@user-EricLin0619
@user-EricLin0619 2 жыл бұрын
@@profgallaugher I have solved the problem, thanks for your reply!!!
Despicable Me Fart Blaster
00:51
_vector_
Рет қаралды 23 МЛН
Became invisible for one day!  #funny #wednesday #memes
00:25
Watch Me
Рет қаралды 59 МЛН
🤔Какой Орган самый длинный ? #shorts
00:42
My NEW Raspberry Pi Music Streamer
8:13
Michael Andrew
Рет қаралды 71 М.
How to use a Raspberry PI as a synthesizer
16:01
Floyd Steinberg
Рет қаралды 98 М.
How To Set Up An IQaudIO DAC+ With Volumio and Raspberry Pi OS
11:55
Core Electronics
Рет қаралды 93 М.
I Can Save You Money! - Raspberry Pi Alternatives
15:04
Linus Tech Tips
Рет қаралды 3,3 МЛН
Raspberry Pi Battery Power
20:34
ExplainingComputers
Рет қаралды 338 М.
Audio modules for electronics project
37:54
Playful Technology
Рет қаралды 42 М.
Control you Raspberry Pi with a BUTTON
12:03
Sam Westby Tech
Рет қаралды 17 М.