python - How can I play a sound through python regardless of file location?
问题描述
I'm trying to play music through python:
import winsound
winsound.PlaySound("A", winsound.SND_FILENAME)
winsound.PlaySound("C", winsound.SND_FILENAME)
winsound.PlaySound("D", winsound.SND_FILENAME)
winsound.PlaySound("D", winsound.SND_FILENAME)
which works, but only while the python file is in the same folder as the sounds. Can I get it to play the sounds from anywhere? Or how do I properly import the sound from a specific path without the python file being with the sound files?
I tried:
winsound.PlaySound("C:\Users\User_Name\Desktop\Microbit\A",
winsound.SND_FILENAME)
I haven't done any coding in a while and I can't remember how to import things properly.
解决方案
不熟悉windsound库,但通常需要转义'\'。我想您还需要字符串末尾的文件类型(.mp3 等)尝试:
winsound.PlaySound("C:\\Users\\User_Name\\Desktop\\Microbit\\A.file_type", #.mp3, .wav, etc
winsound.SND_FILENAME)
推荐阅读
- gmail-api - Gmail API / Google Auth - 如何获得验证?
- javascript - 焦点方法触发按钮点击事件
- java - 在动作监听器中找不到符号错误
- flutter - 使用颤振使用面部ID登录
- azure - Azure 数据工厂 V2 仅在当前执行完成时触发管道
- python - 带有 Flask App 的 Raspberry Pi 实时视频流适用于默认开发服务器,但不适用于 Apache 生产
- c# - c# winforms项目中表格布局打印收据
- django - 在管理员之外使用 Django-Admin 的创建/更新/删除模板?
- java - Android 打印字体错误
- c# - selenium chrome scrape 标记名值 c#