Python Forum
QTimer with pause/resume functions?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QTimer with pause/resume functions?
#1
Hey there, I'm making a music player so have to use a QTimer and set its interval to the length of the current song in order to know when to automatically play the next song in the playlist when the current one ends. This part works perfectly. But I have a problem if I pause the audio:

When I press the pause button and the audio file pauses, the timer does not! So this means that when I resume the song, the app will switch to the next one before the current one has ended as the QTimer has not been paused when I paused the song itself via the button. I found this code but am getting an OverflowError at the very last line where self.start is called in the resume function. Can anybody tell if it's even possible to pause/resume a QTimer (not natively I guess, but with some 3rd party script/override). Or if you have another idea about how to automatically know when the current song has ended - I'm open to that as well.
Reply
#2
Why wouldn't you want to stop the timer via the pause method?
you will find it in the documentation: https://srinikom.github.io/pyside-docs/P...Timer.html
Or is there a problem, so that you can not use that?
Reply
#3
(May-17-2018, 12:48 PM)ThiefOfTime Wrote: Why wouldn't you want to stop the timer via the pause method?
you will find it in the documentation: https://srinikom.github.io/pyside-docs/P...Timer.html
Or is there a problem, so that you can not use that?

Well, if I stop the timer altogether when I pause a song then it will be gone and the app won't know when the song ends so it won't play the next one. That's why I'm looking for a way to 'pause' the timer aka for it to remember its position so that I can later resume it from the old position until the initially set time runs out.
Reply
#4
Ah I see what you mean. Aren't you using the phonon seek slider?
Reply
#5
(May-17-2018, 02:12 PM)ThiefOfTime Wrote: Ah I see what you mean. Aren't you using the phonon seek slider?

No. Phonon didn't work for some reason. But this has nothing to do with a slider xd
Reply
#6
Oh ok, what is the function of your timer precisely? :)
Reply
#7
(May-17-2018, 03:27 PM)ThiefOfTime Wrote: Oh ok, what is the function of your timer precisely? :)

The one which I found but gave OverflowError on the last line is this:
from PySide.QtCore import QTimer
import time

class QTimerWithPause(QTimer):
	def __init__(self, parent = None):
		QTimer.__init__(self, parent)
		self.startTime = 0
		self.interval = 0
	
	def startTimer(self, interval):
		self.interval = interval
		self.startTime = time.time()
		QTimer.start(self, interval, True)

	def pause(self):
		if self.isActive():
			self.stop()
			elapsedTime = self.startTime - time.time()
			self.startTime -= elapsedTime
			self.interval -= int(elapsedTime * 1000)
		
	def resume(self):
		if not self.isActive():
			self.start(self.interval)
Otherwise I just use a normal QTimer to know when to play the next song. But I can't pause it, which is the problem. Here it is:
self.songFileToGetLengthFrom = MP3(self.playlist[self.playlistCurrentSongIndex].path)
			self.songFileLength = self.songFileToGetLengthFrom.info.length
			if self.nextSongTimer:
				self.nextSongTimer.stop()
				self.nextSongTimer.deleteLater()
			self.nextSongTimer = QTimer()
			self.nextSongTimer.timeout.connect(self.playNextSongInPlaylist)
			self.nextSongTimer.setSingleShot(True)
			self.nextSongTimer.start(self.songFileLength * 1000)
And the function that's connected to the timer's end:
def playNextSongInPlaylist(self):
		if self.playlistCurrentSongIndex + 1 < (len(self.playlist) - 1):
			self.playlistCurrentSongIndex += 1
			self.playAudioFromSelectedFile(self.playlist[self.playlistCurrentSongIndex].path)
		else:
			pygame.mixer.music.stop()
			self.playPauseButton.setIcon(self.playIcon)
			self.isPlaying = False
Reply
#8
Hmmmm, I would suggest, that you make a counter and a timer that runs each second. Let's say you know your song is 1 minute and 20 seconds long, then you know that when your counter reaches 80 (or 0 depending which way you prefere) you play the next song. Each second the timer times out and modifies the counter.

what exacly is the problem with phonon? what you are currently trying is a huge workaround.
Reply
#9
(May-17-2018, 03:53 PM)ThiefOfTime Wrote: Hmmmm, I would suggest, that you make a counter and a timer that runs each second. Let's say you know your song is 1 minute and 20 seconds long, then you know that when your counter reaches 80 (or 0 depending which way you prefere) you play the next song. Each second the timer times out and modifies the counter.

what exacly is the problem with phonon? what you are currently trying is a huge workaround.

I can't remember but installation went fine and then it gave errors when I tried to use it I think. And, nice idea I guess. We'll see how it goes in the coming days :D Something like this?
self.timerCounter = 0
self.songFileToGetLengthFrom = MP3(self.playlist[self.playlistCurrentSongIndex].path)
self.songFileLength = self.songFileToGetLengthFrom.info.length
self.nextSongTimer = QTimer()
self.nextSongTimer.timeout.connect(self.playNextSongInPlaylist)
self.nextSongTimer.start(1000)

def playNextSongInPlaylist(self):
    self.timerCounter += 1
    if self.timerCounter == self.songFileLength:
        if self.playlistCurrentSongIndex + 1 < (len(self.playlist) - 1):
            self.playlistCurrentSongIndex += 1
            playAudioFromSelectedFile(self.playlist[self.playlistCurrentSongIndex].path)
And then I just add an additional if to check if isPlaying is true, if it is to increment the counter - otherwise no. This may work, I hope it does :)
Reply
#10
Counter method worked fine :) Problem fixed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  saving progress, go to a new screen, then resume petomane 8 2,787 Jan-11-2021, 02:25 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020