使用python播放.mp4并检查是否/仍在播放 [英] Play .mp4 using python and check if/while it is still playing

查看:333
本文介绍了使用python播放.mp4并检查是否/仍在播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows 64位.我已经尝试了几个库.无法让pygame工作,无法让pymedia安装在python 2.7上.

I'm using Windows 64 bit. I have tried several libraries. Couldnt' get pygame to work, couldnt' get pymedia to install on python 2.7.

最终获得了适用于python的mplayer.

Eventually got 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命令才能播放音频.

For some reason you have to call the pause command to get the audio to play.

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

The major problem arises when I want to start another sound playing. If I just call loadfile on another file it will already be playing so calling the the pause method will pause it rather than play it. If the first file has finished playing, the pause has to be called to play it.

另外,mplayer似乎在音频文件的末尾添加了有线跳转...但是我想我可以接受.

Additionally mplayer appears to add in a wired jump at the end of the audio file... but I guess I can live with that if I have to.

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

So I need some way of checking if the current file is still being played.

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

The library doesn't appear to have a method for this.

有更好的方法吗?

推荐答案

由于此实现的流式传输特性和缺乏文档,所以这样做有点尴尬.

It's a little awkward to do this, due to the streaming nature of this implementation, and the lack of documentation.

但是,这是您的操作方式:

However, this is how you do it:

    p = 'C:\\mymusic.mp4'
    v = VideoPlayback_MPlayer.FromPath(p)
    v.playAsync()
    while v.isPlaying:
        time.sleep(0.1)

您有这样的视频播放器课程:

Where you have a video player class like this:

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播放.mp4并检查是否/仍在播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆