我使用的是 Windows 64 位。我试过几个图书馆。无法让 pygame 工作,无法让 pymedia 安装在 python 2.7 上。
最终得到了 python 的 mplayer。
已安装 https://pypi.python.org/pypi/mplayer.py/
我可以得到一个声音文件来播放
import mplayer
p = = mplayer.Player(args=(), stdout=mplayer.PIPE, stderr=None, autospawn=True)
p.loadfile('C:\mymusic.mp4')
p.pause()
出于某种原因,您必须调用暂停命令才能播放音频。
主要问题出现在我想开始播放另一个声音时。如果我只是在另一个文件上调用 loadfile,它将已经在播放,因此调用 pause 方法将暂停它而不是播放它。如果第一个文件播放完毕,则必须调用暂停来播放它。
此外,mplayer 似乎在音频文件末尾添加了有线跳转...但我想如果必须的话我可以接受。
所以我需要一些方法来检查当前文件是否仍在播放。
图书馆似乎没有这种方法。
有更好的方法吗?
最佳答案
由于此实现的流媒体特性以及缺乏文档,这样做有点尴尬。
但是,这就是您的做法:
p = 'C:\\mymusic.mp4'
v = VideoPlayback_MPlayer.FromPath(p)
v.playAsync()
while v.isPlaying:
time.sleep(0.1)
如果你有这样的视频播放器类:
class VideoPlayback_MPlayer:
def __init__(self, path):
self.path = path
def playAsync(self):
import mplayer #pip install mplayer.py and also setup choco install mplayer myself via http://downloads.sourceforge.net/project/mplayer-win32/MPlayer%20and%20MEncoder/r37451%2Bg531b0a3/MPlayer-x86_64-r37451%2Bg531b0a3.7z?r=http%3A%2F%2Foss.netfarm.it%2Fmplayer%2F&ts=1442363467&use_mirror=tcpdiag
self.isPlaying = True
EOFDetectionArgs = "-msglevel global=6"
self.player = mplayer.Player(args=EOFDetectionArgs.split(), stderr=None, autospawn=True)
self.player.stdout.connect(self._EOFDetector)
self.player.loadfile(self.path)
self.player.pause() # someone says online this kicks in the audio http://stackoverflow.com/questions/16385225/play-mp4-using-python-and-check-if-while-it-is-still-playing
def _EOFDetector(self, stream):
if stream.startswith('EOF code:'):
self.isPlaying = False
@property
def done(self):
return not self.isPlaying
def play(self):
self.playAsync()
while self.isPlaying:
time.sleep(0.00001)
@staticmethod
def FromPath(path):
return VideoPlayback_MPlayer(path)
关于python - 使用 python 播放 .mp4 并检查它是否/仍在播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16385225/