1

I making a video player with pyqt5 but the problem is it cant run a video which is not complete or used by some other process like currently downloading or decrypting.So are there any way to make a player like WhatsApp or YouTube I mean it will fetch the data what we already have and then load the rest.for that time if a user try to slide using slider it will buffer(show loading screen) like WhatsApp or YouTube player.I don't have any idea how to do that.but I am very curious to do that in pyqt5.Any help will be appreciated.

EDIT-

I have found some basic idea that using QBuffer fromPyQt5 : QMediaPlayer can't replay audio from QBuffer and how to read video data from memory use pyqt5 and implemented on my code but it plays for small file but not large file even using Qthread it just crashes.I dont know whether it is the only way to achieve that or some better ways are there to acheive that.Here is my code.

from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import QDir, Qt, QUrl
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
        QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon
import sys     
class Worker(QtCore.QObject):
    word=QtCore.pyqtSignal(str)
    def encrypt(self,path,obj):
        try:
            with open(path, 'rb') as stream:
                    obj.setData(stream.read())
            if obj.open(QtCore.QIODevice.ReadOnly):
                self.word.emit('hi')
        except Exception as e:
                print(e)        
class Player(QMainWindow):
    speak=QtCore.pyqtSignal(str,object)
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("PyQt Video Player Widget Example") 

        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)

        videoWidget = QVideoWidget()

        self.playButton = QPushButton()
        self.playButton.setEnabled(False)
        self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
        self.playButton.clicked.connect(self.play)

        self.positionSlider = QSlider(Qt.Horizontal)
        self.positionSlider.setRange(0, 0)
        self.positionSlider.sliderMoved.connect(self.setPosition)

        self.errorLabel = QLabel()
        self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
                QSizePolicy.Maximum)

        # Create new action
        openAction = QAction(QIcon('open.png'), '&Open', self)        
        openAction.setShortcut('Ctrl+O')
        openAction.setStatusTip('Open movie')
        openAction.triggered.connect(self.openFile)

        # Create exit action
        exitAction = QAction(QIcon('exit.png'), '&Exit', self)        
        exitAction.setShortcut('Ctrl+Q')
        exitAction.setStatusTip('Exit application')
        exitAction.triggered.connect(self.exitCall)

        # Create menu bar and add action
        menuBar = self.menuBar()
        fileMenu = menuBar.addMenu('&File')
        #fileMenu.addAction(newAction)
        fileMenu.addAction(openAction)
        fileMenu.addAction(exitAction)

        # Create a widget for window contents
        wid = QWidget(self)
        self.setCentralWidget(wid)

        # Create layouts to place inside widget
        controlLayout = QHBoxLayout()
        controlLayout.setContentsMargins(0, 0, 0, 0)
        controlLayout.addWidget(self.playButton)
        controlLayout.addWidget(self.positionSlider)

        layout = QVBoxLayout()
        layout.addWidget(videoWidget)
        layout.addLayout(controlLayout)
        layout.addWidget(self.errorLabel)

        # Set widget to contain window contents
        wid.setLayout(layout)

        self.mediaPlayer.setVideoOutput(videoWidget)
        self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
        self.mediaPlayer.positionChanged.connect(self.positionChanged)
        self.mediaPlayer.durationChanged.connect(self.durationChanged)
        self.mediaPlayer.error.connect(self.handleError)
#######
#Here is the main part
#####
        self.worker = Worker()
        self.speak.connect(self.worker.encrypt)
        self.thread=QtCore.QThread(self)
        self.worker.moveToThread(self.thread)
        self.worker.word.connect(self.ready)
        self.buffer = QtCore.QBuffer()
    def ready(self):
        print('Ready')  
        self.mediaPlayer.setMedia(
                    QMediaContent(), self.buffer)
                  
    def openFile(self):
        fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
                "C:/Users/mishra/Desktop/HiddenfilesWindow/")
        print(fileName)
        if fileName != '':
            try:
                self.buffer.close()
                self.thread.start()
                self.speak.emit(fileName,self.buffer)
                # self.mediaPlayer.setMedia(
                #         QMediaContent(QUrl.fromLocalFile(fileName)))
                self.playButton.setEnabled(True)
            except Exception as e:
                print(e)

    def exitCall(self):
        sys.exit(app.exec_())

    def play(self):
        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
            self.mediaPlayer.pause()
        else:
            self.mediaPlayer.play()

    def mediaStateChanged(self, state):
        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
            self.playButton.setIcon(
                    self.style().standardIcon(QStyle.SP_MediaPause))
        else:
            self.playButton.setIcon(
                    self.style().standardIcon(QStyle.SP_MediaPlay))

    def positionChanged(self, position):
        self.positionSlider.setValue(position)

    def durationChanged(self, duration):
        self.positionSlider.setRange(0, duration)

    def setPosition(self, position):
        self.mediaPlayer.setPosition(position)

    def handleError(self):
        self.playButton.setEnabled(False)
        self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())
          
if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Player()
    window.show()
    sys.exit(app.exec_())

This is a basic media player code with some basic functions.

Raj1234
  • 186
  • 10
  • Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of what you've done so far. – musicamante Nov 19 '20 at 10:51
  • I have posted.Basically i have edited with a basic mediaplayer code.please check. @musicamante – Raj1234 Nov 19 '20 at 12:21

0 Answers0