Python - Play Audio Files on Windows Without Install Another Libraries

在不安装其它库的情况下用 Python 播放音频文件。

Tested Environment: Windows 11 + Python v3.11.2

Foreword

In the past few days, I've got into the project which was allocated by my school. I persuade the teachers to use Python instead of Mind+, and given that no one around here knows Python, all of a sudden the pressure was on me. Now, the progress of the project is coming to the end, and it's time to sum up and share some work notes on my blog.

It was a simple project to identify numbers in a photo, and play a corresponding audio file on the local host according to the recognization result. As for the scanning, I used API provided by Baidu. But I got stuck in play sound.

Normally, we may use installed video/audio players to play a media file, but it will open a window. My purpose is to play the audio automatically without opening any visible window. The following content could help you to solve problems similar to this.

Solutions

I have searched the question on the web, most of them require us to install other libraries. I will also list them with example codes first, to provide more options for you.

Lib: playsound

The lib playsound is the easiest way to play a audio, and it is also very lightweight. It supported .mp3 and .wav and more formats.

For install it, you are required to type and run the command pip install playsound in cmd or powershell.

Then you can add the the following code in your program to realize the function of play an audio:

from playsound import playsound
playsound('/path/to/a/sound/file/you/want/to/play.mp3')

Lib: pydub

Pydub lets you do stuff to audio in a way that isn't stupid.

Lib pydub is a high-level library to manipulate audio. It supported many operations that another professional software could do (like Adobe Audition and Adobe Premiere).

While I am developing the program, I've used it to join multiple audios. I won't discuss how to use it for related operations here for now since it's not relevant to what's being discussed in this article (a follow-up article will summarize it).

Install pydub: pip install pydub.

The example code was copied from Stack Overflow. It seems that will occur a bug on my machine, and it need users to install and configure ffmpeg before using, so I don't recommand to use it.

from pydub import AudioSegment
from pydub.playback import play

song = AudioSegment.from_wav("explosion.wav")
play(song)

In the environment of no ffmpeg, pydub only supported operation of .wav format.

No External Lib

In this section, we will introduce a built-in function provided by Python – or, directly say that's provided by Microsoft Windows. Now, we invite this guy nearly been gave up by MS – mci.

What we need just the windll.winmm.mciSendStringW function. For use it, please add the following code in your project.

from ctypes import windll

def MciSendString(command):
    windll.winmm.mciSendStringW(command, 0, 0, 0)

Then, we can control local media files by call MciSendString function and enter some command parameters.

Don't be afraid, there will no too-difficult commands here (at least esay than CMD).

For more advanced functions, I recognize you to have a read at: https://learn.microsoft.com/en-us/windows/win32/multimedia/mci-command-strings.

Here are some common operations if we want to play a mp3:

  • Open and play a audio, wait it finished playing

    open "filename.mp3" alias music
    play music wait
    close music
  • Set volume (range from 1 to 1000, corresponding system volume 1 to 100)

    setaudio music volume to 500
  • Pause & Resume

    pause music
    resume music

Example Code:

# --Snip--
import time

MciSendString('open "filename.mp3" alias music')
MciSendString('play music wait')
MciSendString('close music')

###########################################
MciSendString('open "filename.mp3" alias music')
MciSendString('play music')
MciSendString('setaudio music volume to 500')
MciSendString('pause music')
time.sleep(2)
MciSendString('resume music')

Encapsulate into a function:

from ctypes import windll

def MciSendString(command):
    windll.winmm.mciSendStringW(command, 0, 0, 0)

def play(file):
    MciSendString(f'open "{file}" alias sound')
    MciSendString(f'play sound wait')
    MciSendString(f'close sound')

if __name__ == '__main__':
    play('filename.mp3')

End

Hope it can help you!

阅读剩余
THE END