简体   繁体   English

在pydub中播放音频

[英]Playing audio in pydub

How can I play a wav audio after importing it to my code? 将其导入我的代码后如何播放wav音频?

from pydub import AudioSegment  
song = AudioSegment.from_wav("explosion.wav")

If you're just trying to get a quick idea of what your code is doing (in the REPL for instance), you can use pydub.playback: 如果您只是想快速了解代码的作用(例如在REPL中),您可以使用pydub.playback:

from pydub import AudioSegment
from pydub.playback import play

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

If you have pyaudio installed, that will be used; 如果安装了pyaudio ,将使用; it's sometimes tricky to install . 安装有时候很棘手 Otherwise ffplay will be used. 否则将使用ffplay。

ffplay is not part of the standard ffmpeg install on all platforms, so take a look at " Getting ffmpeg set up " in the pydub docs if you're going that route. ffplay不是所有平台上标准ffmpeg安装的一部分,所以如果你要走那条路线,请看pydub文档中的“ 获取ffmpeg设置 ”。

Another caveat: ffplay is going to cause a window to be opened while the sound is playing, it's almost definitely not an acceptable solution for use in production code. 另一个警告:ffplay将导致在播放声音时打开一个窗口,这几乎绝对不是在生产代码中使用的可接受的解决方案。 If you want to play audio in production code you'll want to look at other options. 如果您想在生产代码中播放音频,您将需要查看其他选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM