pythonpyqtpyqt5qmediaplayerqbuffer

PyQt5 : QMediaPlayer can't replay audio from QBuffer


I have a QBuffer in RAM with a temporary wav file, and I want to let the user listen it from any point as many times as (s)he want. But, it only allows playing it once, and doesn't allow replaying it. If I play the audio from a file (QUrl.fromLocalFile), it can replay it. What's the difference? How to fix it?

1) To play the wav file from RAM I use the following code:

    data = b""
    with open(fname, "rb") as file:
        data = file.read()
    buf = QBuffer()
    buf.setData(data) #For debugging. Real buffer is filled differently.
    buf.open(QIODevice.ReadOnly);

    self.mediaPlayer=QMediaPlayer(self)
    self.mediaPlayer.setMedia(QMediaContent(),buf)

Then, if I call self.mediaplayer.play(), it will play the file to the end. But, all subsequent calls to self.mediaplayer.play() have no effect. This is not what I want.

2) If I init mediaplayer from a file, with:

self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile("/home/me/myTestApp/test.wav")))

it works OK - if i call play() after the previous playback is over, QMediaPlayer just repeats the playback.


Solution

  • A QBuffer is an io-device - once you've read it, you need to reset its position in order to read it again. So in your code you will need to do something like this:

        ...
        self._buffer = buf
        self.mediaPlayer=QMediaPlayer(self)
        self.mediaPlayer.setMedia(QMediaContent(), self._buffer)
    
    def play(self):
        self._buffer.seek(0)
        self.mediaPlayer.play()
    

    EDIT:

    After some actual testing, I found that it is only necessary to keep a reference to the buffer in order to replay the audio. The script below is a complete example that works fine for me (on Linux, using the GStreamer backend):

    import sys
    from PyQt5 import QtCore, QtWidgets, QtMultimedia
    
    class Window(QtWidgets.QWidget):
        def __init__(self):
            super(Window, self).__init__()
            self.buttonOpen = QtWidgets.QPushButton('Open', self)
            self.buttonOpen.clicked.connect(self.handleOpen)
            self.buttonPlay = QtWidgets.QPushButton('Play', self)
            self.buttonPlay.clicked.connect(self.handlePlay)
            layout = QtWidgets.QHBoxLayout(self)
            layout.addWidget(self.buttonOpen)
            layout.addWidget(self.buttonPlay)
            self.mediaPlayer = QtMultimedia.QMediaPlayer(self)
            self._buffer = QtCore.QBuffer()
    
        def handlePlay(self):
            if self.buttonPlay.text() == 'Play':
                self.buttonPlay.setText('Stop')
                # self._buffer.seek(0)
                self.mediaPlayer.play()
            else:
                self.buttonPlay.setText('Play')
                self.mediaPlayer.stop()
    
        def handleOpen(self):
            path, ok = QtWidgets.QFileDialog.getOpenFileName(
                self, filter='WAV Files (*.wav)')
            if ok:
                self._buffer.close()
                with open(path, 'rb') as stream:
                    self._buffer.setData(stream.read())
                if self._buffer.open(QtCore.QIODevice.ReadOnly):
                    self.mediaPlayer.setMedia(
                        QtMultimedia.QMediaContent(), self._buffer)
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        window = Window()
        window.setGeometry(600, 100, 200, 50)
        window.show()
        sys.exit(app.exec_())