I am writing a program which I will send to someone as a single .py file. I want to be able to include sound in this program which the person can hear without requiring extra wav/mp3 files as well. Is this possible? I'm open to using external modules, etc, just as long as it can all be included in one file when I send it.
您可以使用库winsound将您的音频作为 .py 文件中的字节字符串包含在内。
winsound
is a standard python library included with all installations for playing audio on Windows systems; therefore, the person to whom you are sending the program will only need to have python installed.
From the winsound
documentation , you can use sounds from the windows registry without any further file dependencies, eg:
import winsound
# Play Windows exit sound.
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
Details of sounds common to all Windows 32 registries are available here . As stated in the docs, most systems have many more sound aliases available.
Alternatively, you could encode your audio file as a byte string and include this in your python module. You can then pass this bytes object to winsound.PlaySound
to play the audio.
Maybe you can use playsound . The code is pretty simple:
from playsound import playsound
playsound('/path/to/a/sound/file/you/want/to/play.mp3')
Just use the playsound function and send a path to your file as parameter and it will play the sound.
I used this library to make an alarm app for my laptop, and it worked. Good luck!!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.