【问题标题】:Play .mp4 using python and check if/while it is still playing使用 python 播放 .mp4 并检查它是否/同时还在播放
【发布时间】:2013-05-05 13:53:32
【问题描述】:

我使用的是 Windows 64 位。我已经尝试了几个库。不能让 pygame 工作,不能让 pymedia 安装在 python 2.7 上。

最终得到了 mplayer for python。

安装了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()

由于某种原因,您必须调用 pause 命令才能播放音频。

当我想开始另一个声音播放时出现了主要问题。如果我只是在另一个文件上调用 loadfile ,它将已经在播放,因此调用 pause 方法将暂停它而不是播放它。如果第一个文件播放完毕,则必须调用 pause 来播放它。

另外,mplayer 似乎在音频文件的末尾添加了一个有线跳转......但我想如果必须的话我可以忍受。

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

该库似乎没有用于此的方法。

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

【问题讨论】:

    标签: python windows audio


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多