python - How to get the total duration of media being played in PyQt4? -


i trying write small audio player in pyqt4. part of code i've written:

class player(qmainwindow):      def __init__(self, fileloc, parent = none):         super(qmainwindow, self).__init__()         self.tottime = 0         self.audiooutput = phonon.audiooutput(phonon.musiccategory, self)         self.mediaobject = phonon.mediaobject(self)          self.mediaobject.settickinterval(1000)          self.mediaobject.tick.connect(self.tick)         self.mediaobject.statechanged.connect(self.statechanged)          phonon.createpath(self.mediaobject, self.audiooutput)          #define play, pause , stop actions         self.playaction = qaction(self.style().standardicon(qstyle.sp_mediaplay),                             "play", self, enabled = false, triggered = self.mediaobject.play)          self.pauseaction = qaction(self.style().standardicon(qstyle.sp_mediapause),                             "pause", self, enabled = false, triggered = self.mediaobject.pause)          self.stopaction = qaction(self.style().standardicon(qstyle.sp_mediastop),                             "stop", self, enabled = false, triggered = self.mediaobject.stop)          #initiate user interface         self.userinterface()         self.timedisp.display('00:00')          self.mediaobject.setcurrentsource(phonon.mediasource(fileloc))         self.mediaobject.play()      def tick(self, time):         self.displaytime = qtime(0, (time / 60000) % 60, (time / 1000) % 60)         self.timedisp.display(self.displaytime.tostring('mm:ss')) 

my problem is, unable figure out how total duration of file being played. have tried printing output of mediobject.totaltime() @ end of init(). returned -1 videos. mediobject.totaltime() inside tick() returning incorrect duration (10 - 15 seconds longer actual duration).

also, may have access value of total duration outside class. how can this?

thanks in advance.

you connect pause, play, , stop actions other functions:

in class __init__:

self.total_time = 0 self.playing = false self.play_action = qaction(self.style().standardicon((qstyle.sp_mediaplay),"play",self)  self.play_action.triggered.connect(self.play_triggered_event) 

and define rest of actions in similar manner, connecting triggered field of each qaction function

def play_triggered_event(self):     if not self.playing:         self.mediaobject.play         self.playing = true         self.start_time = time.clock()  def pause_triggered_event(self):     if self.playing:         self.mediaobject.pause         self.playing = false         self.total_time += (time.clock() - self.start_time)  def stop_triggered_event(self):     if self.playing:        self.mediaobject.stop        self.playing = false        print "total time elapsed: " + str(self.total_time) 

basically, it's saving program state when actions affect time elapsed triggered.

to total time outside of class, write accessor class:

def get_total_time(self):     return self.total_time 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -