Python Forum
QTimer with pause/resume functions?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QTimer with pause/resume functions?
#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


Messages In This Thread
QTimer with pause/resume functions? - by MegasXLR - May-17-2018, 12:26 PM
RE: QTimer with pause/resume functions? - by MegasXLR - May-17-2018, 03:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  saving progress, go to a new screen, then resume petomane 8 2,919 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