python - 使用 python 播放 .mp4 并检查它是否/仍在播放

标签 python windows audio

我使用的是 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/

相关文章:

objective-c - 使用音频队列服务的示例

python - 如何使用 SQLAlchemy 和 Flask 对两个表执行自然连接?

python - 如何根据下限和上限调用 pandas DataFrame 中的数据?

windows - 我可以在全局范围内使用 TortoiseGit 注册 PuTTY key 吗(不是每个存储库)?

audio - Flutter-如何播放环境声音?

java - 如何在Android Studio中设置音频延迟?

python - 将 Python 脚本实现为 C++

python - 缩放Y轴和X轴Python图

windows - 如何保存命令提示符的输出

c++ - 在窗口区域上使用 UpdateLayeredWindow