如何在显示文本的同时在 Python 2.6.6 中播放音频?
我正在尝试使用 Python 2.6.6 制作一个程序,在后台播放音频的同时显示文本。我只完成了部分文字。
print "Well here we are again"
print "It’s always such a pleasure"
print "Remember when you tried to kill me twice?"
print "Oh, how we laughed and laughed"
print "Except I wasn’t laughing"
print "Under the circumstances I’ve been shockingly nice."
我有一个 .wav 文件,我想在程序开始时播放它。我还希望文本与音乐一起播放(我可以指定文本在歌曲中显示的时间,例如 00:00:02)。我认为这可以通过某种音频模块来实现。
谢谢!
I am trying to make a program with Python 2.6.6 that displays text while having audio playing in the background. I have only completed some of the text.
print "Well here we are again"
print "It’s always such a pleasure"
print "Remember when you tried to kill me twice?"
print "Oh, how we laughed and laughed"
print "Except I wasn’t laughing"
print "Under the circumstances I’ve been shockingly nice."
I have a .wav file that I would like to play at the beginning of the program. I would also like the text to play in cue with the music (I would be able to specify when the text displays during the song such as 00:00:02). I assume this would be possible with an audio module of some sort.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近做了类似的事情并使用了audiere模块
这将打开第一个可用的音频设备,因为你在Windows上它可能是DirectSound。
input
只是一个 numpy 数组,fs
是采样频率(因为输入是原始数组,您需要指定它)。os.play()
是一个非阻塞调用,因此您可以打印 txt 或同时执行任何您需要执行的操作,还有其他方法可以暂停/停止等。要播放其他文件类型我只是先将它们转换为 wav 。以下是我解压 wav 文件的方法
例如,要制作
input
和fs
你可以这样做:这适合我,但我的要求是 FFT 输入,而不是卡拉 OK :)
我'我确信只要提供一个 2-dim 数组,
audiere
就能实现立体声播放。I did a similar thing recently and used the
audiere
moduleThis will open the first available audio device, since you're on windows it's probably DirectSound.
input
is just a numpy array,fs
is the sampling frequency (since the input is a raw array you'll need to specify that).os.play()
is a non-blocking call so you can print your txt or whatever you need to do at the same time, there are other methods to pause/stop etc. To play other file types I simply converted them to wav first.Here's how I unpacked the wav file
So for example to make
input
andfs
you could go:This suited me but my requirements were for FFT input, not karaoke :)
I'm sure
audiere
has stereo playback just by giving a 2-dim array.您可能会发现 pygame 有帮助。
http://www.pygame.org/docs/ref/mixer.html
You may find pygame helpful.
http://www.pygame.org/docs/ref/mixer.html
您需要的是适用于 Python 的 GStreamer。
查看此教程,这是一个很好的起点。
编辑:
在Windows中,您可以使用标准库中的winsound模块(哇!Python有这个!)请参阅winsound 文档。
What you need is the GStreamer for Python.
Check this tutorial, it's a good place to start.
EDIT:
In Windows you can use the winsound module from standard library (Whoa! Python has that!) See winsound docs.