Тёмный

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

Prof. John Gallaugher
Подписаться 8 тыс.
Просмотров 21 тыс.
50% 1

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 RU-vid channel. Enjoy & tell others!

Опубликовано:

 

8 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 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 7 месяцев назад
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 7 месяцев назад
This is helpful. Thank you.
@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.
@nayanrimal3206
@nayanrimal3206 Год назад
good job bro
@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!
@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
@richardskidmore6632
@richardskidmore6632 7 месяцев назад
Ps my code is exactly the same but only plays the last in the list. Any idea what I’ve done wrong? Ta. Rik
@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 ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-1FAYy1hrcGc.html
@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.
@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!
@suhasmahajan-if9ez
@suhasmahajan-if9ez 6 месяцев назад
Sir if i will pulg ear phones will it work?
@profgallaugher
@profgallaugher 6 месяцев назад
Try ‘em out, homie!
@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.
@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!!!
Далее
3 HACKING gadgets you have to TRY!!
19:34
Просмотров 1,5 млн
лучшая покупка в моей жизни
00:41
Bike vs Super Bike Fast Challenge
00:30
Просмотров 10 млн
Пришёл к другу на ночёвку 😂
01:00
13 Stunning Raspberry Pi Projects for 2024!!!
10:23
Просмотров 252 тыс.
Control you Raspberry Pi with a BUTTON
12:03
Просмотров 19 тыс.
DIY Raspberry Pi Racecar Dashboard: The Ultimate Guide
15:11
лучшая покупка в моей жизни
00:41