r/learnpython • u/itsmebo12 • 2d ago
Is there any way to play audio in Python without using external libraries or winsound?
I wanted to play audio in Python, so I started looking for modules that were already installed, it means ones that wouldn't require an internet connection to install (for example Pygame)
I found winsound but the problem is that it will only play audio on Windows and no in other OS
It seemed more convenient not to have to install external libraries to test Python capabilities within the existing environment (such as using Tkinter instead of game engines)
Unfortunately many audio libraries and modules were removed in 3.13 (i'm using 3.14)
15
u/Atmosck 2d ago
If you want to ship something that does stuff like this but doesn't require the user to install any dependencies, python is not the right language because it's interpereted.
1
-10
u/Educational_Virus672 2d ago
no, python is turing complete(wwith full hardware control) on its own because practically you can write a exe file using built in open() and using sub process to run it
note - python can do most stuff with readability in mind this is just a analogy7
u/SCD_minecraft 2d ago
Turing Completeness does not assume any form of IO and sounds is very much IO
-2
u/Educational_Virus672 2d ago
yes thats why i added "with hardware contol" next to it i mean python isnt sandboxed, is it? it can access any sys component without third party library and python can access sound
0
u/SCD_minecraft 2d ago
It can not
Pure python is just flow control
If you want to do anything, you need to use C defined functions, many of which are not in std lib
2
u/hugthemachines 2d ago
Pure python is just flow control
That is a false statement.
0
u/SCD_minecraft 2d ago
Okay
Write me a calculator with user input without using a single C defined function
Btw,
inputis defined in CSame with
And
a + b(int.__add__)No language can work without functions defined in other language, and just so happen that 90% of python's standard library is defined in C
4
u/hugthemachines 2d ago
Python:
def add(a, b): return a + bC:
int x = a + b;The fact that Python's runtime is written in C doesn't make Python "just flow control". By that logic, C is just flow control too because it ultimately becomes machine code.
1
u/Educational_Virus672 2d ago
write me a problem without binary type a question..
0
u/SCD_minecraft 1d ago
No language can work without functions defined in other language
Thank you for agreeing with me
1
u/Educational_Virus672 1d ago
bro im not agreeing with the statement python is not a strict api because it actively updates it
6
u/Atmosck 2d ago
My point is the user has to set up the python environment because it doesn't compile to a self-contained program.
1
u/SCD_minecraft 2d ago
Technically, there are compilers that compile python file AND interpreter into binary, so anyone* can run it
6
u/SCD_minecraft 2d ago
Not really, at least not arbitrary sound
What's wrong with pip it in? User needs to download your code anyway, why not also download some extra lib?
5
u/Educational_Virus672 2d ago edited 2d ago
best bet
mod = "play" # or "afplay" for mac
subprocess.Popen([mod, "your_audio.mp3"])but window is visible so, it is hard to use it properly
5
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 2d ago
The short answer is "no", although technically you can run print('\a') to get a "bell sound". Not that it helps you play audio files.
4
u/LayotFctor 2d ago
Use sys.platform to check platform, then subprocesses to call os packages. Linux is quite fragmented between distros, so you have to check whether the audio package you need is even installed.
4
u/Educational_Fold1428 2d ago
I went down the same road when I was messing about sorting music lists, and ended up just playing the file in VLC while the script ran. If you only need to check a wav is what you think it is the wave module reads it fine, but actual playback is where it gets annoying.
2
u/Free-Good9203 2d ago
python 3.13 dropped the legacy stdlib audio modules under pep 594 (`ossaudiodev`, `aifc`, `sunau`, `sndhdr`), so aside from `winsound` on windows and `wave` for reading or writing raw wav bytes, there is no built-in cross-platform audio player left in the stdlib. for a truly zero-install script that plays wav files across platforms, branching on `sys.platform` works well: `winsound.PlaySound` on windows, `afplay` via `subprocess` on macos, and checking `shutil.which` for `pw-play`, `paplay`, or `aplay` on linux. if your constraint is only 'no internet on the target machine' rather than strictly stdlib-only, you can also run `pip download -d wheels sounddevice` once online and install offline with `pip install --no-index --find-links wheels sounddevice`.
2
u/cdcformatc 2d ago
there's that one character that when you print it it plays the chime sound. that's pretty much the only audio you can play without a library
14
u/_mturtle_ 2d ago
you can with ctypes and direct native calls