'PyQt 5 - Slider sets audio to a wrong position

I am working on an music player app using QMediaPlayer. I use Horizional Slider to navigate through the song, however, Horizontal Slider displays incorrect timing, it may also start playing audio from the beginning even though its position is respective to 00:01 - 00:02 positions of the player.

import sys, os
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent

Form, Window = uic.loadUiType("audio_player.ui")

#Set up window
app = QApplication([])
window = Window()
form = Form()
form.setupUi(window)

name = form.label
media = '/path/to/file'
player = QMediaPlayer()
slider = form.horizontalSlider
full_path = os.path.join(self.media)
url = QUrl.fromLocalFile(self.full_path)
song = QMediaContent(self.url)
player.setMedia(self.song)
player.play()
slider.setRange(0, player.duration())

def positionShow():
    slider.setValue(player.position())

def positionChange():
    if slider.value() == 0:
        player.stop()
        slider.setValue(0)
        player.play()
    elif: slider.value() == player.duration():
        player.stop()
        slider.setValue(0)
    else:
        player.setPosition(slider.value())

slider.released.connect(positionChange)
player.positionChanged.connect(positionShow)

window.show()
app.exec()

Adding and substracting some time from slider to line up with audio, however it didn't work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source