更改视频播放速率时,python pyqt5 setPlaybackRate显示黑屏

时间:2018-10-10 08:44:24

标签: python pyqt5 qvideowidget qmediacontent

嗨,我是python的新手,我正在尝试构建一个将充当驾驶模拟器的UI。用户在更改“行驶”速度时应该能够平稳地更改视频速度。 只要用户达到改变视频速度的速度,影片中的黑屏就会显示,一切正常(50,90,110) 任何想法如何解决呢? 附上代码 谢谢!

from time import gmtime, strftime
import threading
import sys

from PyQt5.QtGui import *
from PyQt5 import QtGui
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import (QLCDNumber)
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import ( QLabel)
from PyQt5.QtWidgets import QWidget, QPushButton
from PyQt5.QtCore import pyqtSlot

class Example2(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()


    def initUI(self):
        self.video = False
        self.thread_list = []
        self.response = ""
        self.setStyleSheet("QLabel {font: 20pt Comic Sans MS}")
        lcd = QLCDNumber(self)
        lcd.display(100)
        lcd.setMaximumHeight(100)
        lcd.setDigitCount(6)
        sld = QSlider(Qt.Horizontal, self)
        sld.setMaximum(140)
        sld.setSingleStep(2.5)
        sld.setValue(110)
        lbl = QLabel(self)
        lbl.setText("Please change your speed using the up and down arrows as you see fit")
        fr = QFrame()
        btn=QPushButton("start",self)
        btn2=QPushButton("stop",self)
        w = QVideoWidget()
        w.resize(840, 680)
        w.move(0, 0)
        player = QMediaPlayer()
        url = "video.mp4"
        player.setMedia(QMediaContent(QUrl.fromLocalFile(url)))
        print("player set media")
        player.setVideoOutput(w)

        @pyqtSlot()
        def on_click():
            player.play()
            print('opened: ', str(strftime("%Y-%m-%d %H:%M:%S", gmtime())))

        @pyqtSlot()
        def on_stop():
            print('stopped',str(strftime("%Y-%m-%d %H:%M:%S", gmtime())))
            player.pause()

        def int_save(x):
            d={}
            d[x] = str(strftime("%Y-%m-%d %H:%M:%S", gmtime())) + "  v= "+str(x)
            print(d[x])
            y=player.duration()
            print(y/(1000*60))
            if x < 50:
                player.setPlaybackRate(0.5)
            if x > 50 and x < 90:
                player.setPlaybackRate(1)
            if x > 90 and x < 110:
                player.setPlaybackRate(1.25)
            if x > 110:
                sad = QPixmap('sad.png')

        btn.clicked.connect(on_click)
        btn2.clicked.connect(on_stop)

        grid = QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(lbl, 0,1)
        grid.addWidget(lcd, 3, 1)
        grid.addWidget(w, 1, 0,2,0)

        grid.addWidget(sld, 4, 1)

        grid.addWidget(btn, 5, 1)
        grid.addWidget(btn2, 5,0)

        self.setLayout(grid)

        sld.valueChanged.connect(lcd.display)
        sld.valueChanged.connect(int_save)
        print("done")

        self.setGeometry(900, 900, 900, 900)
        self.setWindowTitle('Signal and slot')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example2()
    sys.exit(app.exec_())

0 个答案:

没有答案