2013-05-05 141 views
2

我使用的是Windows 64位。我试过了几个库。无法'pygame工作,couldnt'python安装在Python 2.7。使用python播放.mp4并检查它是否仍在播放

最终得到了蟒蛇的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,它将已经在播放,所以调用暂停方法将会暂停而不是播放它。如果第一个文件已完成播放,则必须调用暂停以播放它。

此外,mplayer似乎添加在音频文件末尾的有线跳跃......但我想我可以忍受这一点,如果我必须。

所以我需要一些方法来检查当前文件是否仍在播放。

该库似乎没有这方面的方法。

有没有更好的方法来做到这一点?

回答

1

由于此实现的流式性质以及缺少文档,这样做有点尴尬。

不过,你这是怎么做到这一点:

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)