Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PYQt5中的VLC Python中添加计时器?_Python_Pyqt5_Vlc_Libvlc_Vlc Qt - Fatal编程技术网

如何在PYQt5中的VLC Python中添加计时器?

如何在PYQt5中的VLC Python中添加计时器?,python,pyqt5,vlc,libvlc,vlc-qt,Python,Pyqt5,Vlc,Libvlc,Vlc Qt,我已经为媒体播放器Gui使用了VLC Python绑定,我想在下面的滑块上添加计时器,比如0.00:0.00(当前视频时间:视频的总持续时间),我如何添加这个 如何回调当前播放时间并显示为图像中提到的位置滑块下方的标签 请帮助小提示如何做 import sys from PyQt5 import QtCore as qtc from PyQt5 import QtWidgets as qtw from PyQt5 import QtGui as qtg import vlc import os.

我已经为媒体播放器Gui使用了VLC Python绑定,我想在下面的滑块上添加计时器,比如0.00:0.00(当前视频时间:视频的总持续时间),我如何添加这个

如何回调当前播放时间并显示为图像中提到的位置滑块下方的标签

请帮助小提示如何做

import sys
from PyQt5 import QtCore as qtc
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
import vlc
import os.path


class MainWindow(qtw.QMainWindow):
  def __init__(self, parent=None):

      super().__init__(parent)
      ##Main framwork
      # creating a basic vlc instance
      self.instance = vlc.Instance()
      # creating an empty vlc media player
      self.mediaplayer = self.instance.media_player_new()

      self.createUI()
      self.isPaused = False

  def createUI(self):

      base_widget = qtw.QWidget()
      base_widget.setLayout(qtw.QHBoxLayout())
      notebook = qtw.QVBoxLayout()
      base_widget.layout().addLayout(notebook)
      self.setCentralWidget(base_widget)

      #VideoFrame Loading
      self.videoframe = qtw.QFrame()
      self.videoframe.setMinimumWidth(950)
      self.videoframe.setMinimumHeight(525)
      self.palette = self.videoframe.palette()
      self.palette.setColor (qtg.QPalette.Window,
                             qtg.QColor(0,0,0))
      self.videoframe.setPalette(self.palette)
      self.videoframe.setAutoFillBackground(True)

      #Position Slider
      self.positionslider = qtw.QSlider(qtc.Qt.Horizontal, self)
      self.positionslider.setToolTip("Position")
      self.positionslider.setMaximum(100000.0)
      self.positionslider.setTickPosition(qtw.QSlider.TicksBelow)
      self.positionslider.setTickInterval(2000)
      self.positionslider.sliderMoved.connect(self.setPosition)

      self.hbuttonbox = qtw.QHBoxLayout()
      self.playbutton = qtw.QPushButton("Play")
      self.hbuttonbox.addWidget(self.playbutton)
      self.playbutton.clicked.connect(self.PlayPause)

      #Button Box
      self.stopbutton = qtw.QPushButton("Stop")
      self.hbuttonbox.addWidget(self.stopbutton)
      self.stopbutton.clicked.connect(self.Stop)

      #Volume slider
      self.hbuttonbox.addStretch(1)
      self.volumeslider = qtw.QSlider(qtc.Qt.Horizontal, self)
      self.volumeslider.setMaximum(100)
      self.volumeslider.setValue(self.mediaplayer.audio_get_volume())
      self.volumeslider.setToolTip("Volume")
      self.hbuttonbox.addWidget(self.volumeslider)
      self.volumeslider.valueChanged.connect(self.setVolume)

      notebook.addWidget(self.videoframe)
      notebook.addWidget(self.positionslider)
      notebook.addLayout(self.hbuttonbox)

      #Actions Code
      open1 = qtw.QAction("&Open", self)
      open1.triggered.connect(self.OpenFile)

      exit = qtw.QAction("&Exit", self)
      exit.triggered.connect(sys.exit)


      menubar = self.menuBar()
      filemenu = menubar.addMenu("&File")
      filemenu.addAction(open1)
      filemenu.addSeparator()
      filemenu.addAction(exit)

      self.timer = qtc.QTimer(self)
      self.timer.setInterval(200)
      self.timer.timeout.connect(self.updateUI)

  def PlayPause(self):
      """Toggle play/pause status
      """
      if self.mediaplayer.is_playing():
          self.mediaplayer.pause()
          self.playbutton.setText("Play")
          self.isPaused = True
      else:
          if self.mediaplayer.play() == -1:
              self.OpenFile()
              return
          self.mediaplayer.play()
          self.playbutton.setText("Pause")
          self.timer.start()
          self.isPaused = False

  def PausePlay(self):

      if self.mediaplayer.is_playing():
          self.mediaplayer.pause()
          self.playbutton.setText("Play")
          self.isPaused = True



  def Stop(self):
      """Stop player
      """
      self.mediaplayer.stop()
      self.playbutton.setText("Play")


  def OpenFile(self, filename=None):

      """Open a media file in a MediaPlayer
      """
      if filename is None or filename is False:
          print("Attempt to open up OpenFile")
          filenameraw = qtw.QFileDialog.getOpenFileName(self, "Open File", os.path.expanduser('~'))
          filename = filenameraw[0]
      if not filename:
          return
      # create the media
      if sys.version < '3':
          filename = unicode(filename)

      self.media = self.instance.media_new(filename)
      # put the media in the media player
      self.mediaplayer.set_media(self.media)

      # parse the metadata of the file
      self.media.parse()
      # set the title of the track as window title
      self.setWindowTitle(self.media.get_meta(0))
      # print(vlc.libvlc_media_get_meta(self.media, 6))
      # print(vlc.libvlc_media_get_duration(self.media))
      # the media player has to be 'connected' to the QFrame
      # (otherwise a video would be displayed in it's own window)
      # this is platform specific!
      # you have to give the id of the QFrame (or similar object) to
      # vlc, different platforms have different functions for this
      if sys.platform.startswith('linux'):  # for Linux using the X Server
          self.mediaplayer.set_xwindow(self.videoframe.winId())
      elif sys.platform == "win32":  # for Windows
          self.mediaplayer.set_hwnd(self.videoframe.winId())
      elif sys.platform == "darwin":  # for MacOS
          self.mediaplayer.set_nsobject(int(self.videoframe.winId()))
      self.PlayPause()

  def setVolume(self, Volume):
      """Set the volume  """
      self.mediaplayer.audio_set_volume(Volume)


  def setPosition(self, position):
      """Set the position
      """
      # setting the position to where the slider was dragged
      self.mediaplayer.set_position(position / 100000.0)
      # the vlc MediaPlayer needs a float value between 0 and 1, Qt
      # uses integer variables, so you need a factor; the higher the
      # factor, the more precise are the results
      # (1000 should be enough)

  def updateUI(self):
      """updates the user interface"""
      # setting the slider to the desired position
      self.positionslider.setValue(self.mediaplayer.get_position() * 100000.0)

      if not self.mediaplayer.is_playing():
          # no need to call this function if nothing is played
          self.timer.stop()
          if not self.isPaused:
              # after the video finished, the play button stills shows
              # "Pause", not the desired behavior of a media player
              # this will fix it
              self.Stop()


if __name__ == '__main__':
  app = qtw.QApplication(sys.argv) #it's required to save a referance to MainWindow
  mw = MainWindow()
  mw.show()
  if sys.argv[1:]:
      mw.OpenFile(sys.argv[1])
  sys.exit(app.exec_())
  #if it goes out of scope ,it will be destroyed

导入系统 从PyQt5导入QtCore作为qtc 从PyQt5将QtWidgets作为qtw导入 从PyQt5将QtGui作为qtg导入 进口vlc 导入操作系统路径 类主窗口(qtw.QMainWindow): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) ##主框架 #创建基本vlc实例 self.instance=vlc.instance() #创建空的vlc媒体播放器 self.mediaplayer=self.instance.media\u player\u new() self.createUI() self.isPaused=False def createUI(自): base_widget=qtw.QWidget() base_widget.setLayout(qtw.QHBoxLayout()) notebook=qtw.QVBoxLayout() base_widget.layout().addLayout(笔记本电脑) self.setCentralWidget(基本窗口小部件) #视频帧加载 self.videoframe=qtw.QFrame() self.videoframe.setMinimumWidth(950) 自我视频帧设置最小高度(525) self.palete=self.videoframe.palete() self.palete.setColor(qtg.qpalete.Window, qtg.QColor(0,0,0)) self.videoframe.setPalette(self.palette) self.videoframe.setAutoFillBackground(真) #位置滑块 self.positionslider=qtw.QSlider(qtc.Qt.Horizontal,self) self.positionslider.setToolTip(“位置”) self.positionslider.setMaximum(100000.0) self.positionslider.setTickPosition(qtw.QSlider.TicksBelow) self.positionslider.setTickInterval(2000) self.positionslider.sliderMoved.connect(self.setPosition) self.hbuttonbox=qtw.QHBoxLayout() self.playbutton=qtw.QPushButton(“播放”) self.hbuttonbox.addWidget(self.playbutton) self.playbutton.clicked.connect(self.PlayPause) #按钮盒 self.stopbutton=qtw.QPushButton(“停止”) self.hbuttonbox.addWidget(self.stopbutton) self.Stop按钮。单击。连接(self.Stop) #音量滑块 self.hbuttonbox.addStretch(1) self.volumeslider=qtw.QSlider(qtc.Qt.Horizontal,self) 自体积滑块设置最大值(100) self.volumeslider.setValue(self.mediaplayer.audio\u get\u volume()) self.volumeslider.setToolTip(“卷”) self.hbuttonbox.addWidget(self.volumeslider) self.volumeslider.valueChanged.connect(self.setVolume) notebook.addWidget(self.videoframe) notebook.addWidget(self.positionslider) notebook.addLayout(self.hbuttonbox) #动作代码 open1=qtw.QAction(“&Open”,self) open1.triggered.connect(self.OpenFile) exit=qtw.QAction(“&exit”,self) 退出。触发。连接(系统退出) menubar=self.menubar() filemenu=menubar.addMenu(“&File”) filemenu.addAction(open1) filemenu.addSeparator() filemenu.addAction(退出) self.timer=qtc.QTimer(self) 自动定时器设置间隔(200) self.timer.timeout.connect(self.updateUI) def播放暂停(自我): “”“切换播放/暂停状态 """ 如果self.mediaplayer.正在播放(): self.mediaplayer.pause() self.playbutton.setText(“播放”) self.isPaused=True 其他: 如果self.mediaplayer.play()=-1: self.OpenFile()文件 返回 self.mediaplayer.play() self.playbutton.setText(“暂停”) self.timer.start() self.isPaused=False def暂停播放(自): 如果self.mediaplayer.正在播放(): self.mediaplayer.pause() self.playbutton.setText(“播放”) self.isPaused=True def停止(自): “停止播放 """ self.mediaplayer.stop() self.playbutton.setText(“播放”) def OpenFile(self,filename=None): “”“在MediaPlayer中打开媒体文件 """ 如果filename为None或filename为False: 打印(“尝试打开OpenFile”) filenameraw=qtw.QFileDialog.getOpenFileName(self,“打开文件”,os.path.expanduser(“~”) filename=filenameraw[0] 如果不是文件名: 返回 #创建媒体 如果sys.version<'3': filename=unicode(文件名) self.media=self.instance.media\u new(文件名) #将媒体放入媒体播放器 self.mediaplayer.set_媒体(self.media) #解析文件的元数据 self.media.parse() #将曲目标题设置为窗口标题 self.setWindowTitle(self.media.get_meta(0)) #打印(vlc.libvlc_media_get_meta(self.media,6)) #打印(vlc.libvlc_媒体_获取_持续时间(self.media)) #媒体播放器必须“连接”到QFrame #(否则视频将显示在其自己的窗口中) #这是特定于平台的! #您必须将QFrame(或类似对象)的id提供给 #vlc,不同的平台对此有不同的功能 如果sys.platform.startswith('linux'):#对于使用X服务器的linux self.mediaplayer.setxwindow(self.videoframe.winId()) elif sys.platform==“win32”:#对于Windows self.mediaplayer.set\u hwnd(self.videoframe.winId()) elif sys.platform==“darwin”:#对于MacOS self.mediaplayer.set\n对象(int(self.videoframe.winId()) self.PlayPause() def设置音量(自身,音量): “”“设置音量”“” self.mediaplayer.audio\u set\u volume(音量) def设置位置(自身,位置): “”“设置位置 """ #设置拖动滑块的位置 self.mediaplayer.set_位置(位置/100000.0) #vlc MediaPlayer需要介于0和1 Qt之间的浮点值 #使用整数变量,因此需要一个因子;越高 #因子,结果越精确