Welcome to the CSC Q&A.
Get help and give help!
Write great code!
It is our choices... that show what we truly are, far more than our abilities.


+5 votes

Hey everyone, for my final project I would like to include some audio. I wanted to use a picture of a speaker (like the one that usually pops up when you turn your volume up or down o your Mac laptop for example) as the button that you click in order to hear the audio. I already have a button class, but I just wanted to make sure that I would need to create a new ImageButton class correct? If so what are some helpful parameters that you can think of that I would have to pass in? The ones I thought of this far are the center point of where I want the picture to appear and the filename of the picture.

Also, I was wondering if anyone had any tips on how to write code in terms of actually opening up audio files. I did some online research and most of the sources tells me that I would have to convert my audio file into a .wav or .mp3 file and then use a downloaded package or something like that to actually open the file and play it. I was wondering if anyone had any other ideas? Thanks!

asked in CSC201 Spring 2021 by (8 points)

2 Answers

+2 votes

You will need to use 3rd party packages to process the audio file because python doesn't support audio processing in built-in packages. You can either use pygame, Tkinter, or pydub as shown here https://pythonbasics.org/python-play-sound/.
if you only want to add the enable/disabled audio feature then you can do methods start_audio stop_audio

answered by (8 points)
+2 votes

If your audio file is a .mp3 file, try this:

import os
os.system('afplay file_name.mp3')
answered by (381 points)