macos 如何在 Mac 上从 Python 播放 WAV 或 MP3 音频文件触发?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3498313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:08:58  来源:igfitidea点击:

How to trigger from Python playing of a WAV or MP3 audio file on a Mac?

pythonmacosaudio

提问by GJ.

I'm looking for an elegant way, without a ton of dependencies as in some of the solutions I googled up.

我正在寻找一种优雅的方式,没有像我在 google 上搜索到的一些解决方案那样的大量依赖项。

Thanks for any ideas.

感谢您的任何想法。

采纳答案by GJ.

As far as I know PyGame is the most portable way to play music: http://www.pygame.org/docs/ref/music.html

据我所知 PyGame 是最便携的播放音乐的方式:http: //www.pygame.org/docs/ref/music.html

You can find its package here: http://www.pygame.org/download.shtml

你可以在这里找到它的包:http: //www.pygame.org/download.shtml

回答by Andrew

If you want to do away with external dependencies entirely, and are running OS X 10.5+, you can use the included command-line audio player, afplay, along with the subprocessmodule.

如果你想弄死外部依赖完全,并且运行OS X 10.5以上版本,您可以使用附带的命令行的音频播放器,afplay,与沿模块。

I haven't tested it, but this should work:

我还没有测试过,但这应该有效:

import subprocess
audio_file = "/full/path/to/audio.wav"

return_code = subprocess.call(["afplay", audio_file])

回答by user2682863

The leanest most portable way I've found to play .mp3 and .wav files is playsound.

我发现播放 .mp3 和 .wav 文件的最精简、最便携的方式是playsound

import playsound

# wait for the sound to finish playing?
blocking = True

playsound.playsound("yourfile.mp3", block=blocking)