How to Play Sounds in Python with Minimum Dependencies
Playing sound files in Python can be simple with the right approach. While frameworks like Pygame offer comprehensive functionality, they can come with additional dependencies. To minimize these dependencies, explore the following platform-specific options:
Windows
Windows has built-in support for playing sounds through the winsound module. Here's an example:
<code class="python">import winsound winsound.PlaySound('sound.wav', winsound.SND_FILENAME)</code>
Linux
On Linux, you can leverage the ossaudiodev library:
<code class="python">from wave import open as waveOpen from ossaudiodev import open as ossOpen s = waveOpen('tada.wav', 'rb') ... # Set sound parameters and play the sound</code>
Cross-Platform Alternative
If you need a truly cross-platform solution, you can use a sound library such as soundfile or wave, which provides a more portable way to handle sound files across different platforms. However, these libraries may require additional dependencies.
The above is the detailed content of How to Play Sounds in Python with Minimal Dependencies?. For more information, please follow other related articles on the PHP Chinese website!