我尝试使用 pip install Playsound 下载最新版本的 Playsound
File "C:\Users\Farhad Rashid Reshi\PycharmProjects\PythonHarry\hello_world.py", line 13, in <module>
play soundd('C:\\Users\\Farhad Rashid Reshi\\Downloads\\audio.mp3');
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Farhad Rashid Reshi\PycharmProjects\PythonHarry\venv\Lib\site-packages\playsound.py", line 44, in _playsoundWin
_playsoundWin(tempPath, block)
File "C:\Users\Farhad Rashid Reshi\PycharmProjects\PythonHarry\venv\Lib\site-packages\playsound.py", line 73, in _playsoundWin
in commandmmplayplay {}{}'.format(sound, ' wait' if block else ''))
File "C:\Users\Farhad Rashid Reshi\PycharmProjects\PythonHarry\venv\Lib\site-packages\playsound.py", line 64, in winCommand
raise PlaysoundExcexception messageMessage)
playsound.PlaysoundException:
Error 259 for command:
play "C:\Users\FARHAD~1\AppData\Local\Temp\PSaamnph4b.mp3" wait
The driver cannot recognize the specified command parameter.
我已经尝试了解决问题的方法,但仍然面临同样的错误。
要在Python中播放音频,您可以使用pygame库。以下是如何安装它并在 Python 脚本中播放音频的方法:
安装pygame:
您可以使用 Python 包管理器 pip 安装 pygame。打开终端或命令提示符并运行以下命令:
pip install pygame
编写Python脚本来播放音频:
这是一个使用 pygame 播放音频文件的简单示例脚本:
import pygame
def play_audio(file_path):
# Initialize Pygame mixer
pygame.mixer.init()
# Load the audio file
pygame.mixer.music.load(file_path)
# Play the audio file
pygame.mixer.music.play()
# Keep the script running while the audio is playing
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
if __name__ == "__main__":
audio_file = "path/to/your/audio/file.mp3" # Change this to the path of your audio file
play_audio(audio_file)
将“path/to/your/audio/file.mp3”替换为音频文件的路径。
运行Python脚本:
使用 .py 扩展名保存脚本(例如 play_audio.py)。然后,使用 Python 解释器运行脚本: