當前位置: 首頁>>代碼示例>>Python>>正文


Python Book.set_progress方法代碼示例

本文整理匯總了Python中book.Book.set_progress方法的典型用法代碼示例。如果您正苦於以下問題:Python Book.set_progress方法的具體用法?Python Book.set_progress怎麽用?Python Book.set_progress使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在book.Book的用法示例。


在下文中一共展示了Book.set_progress方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Player

# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import set_progress [as 別名]

#.........這裏部分代碼省略.........
        self.status_light.interrupt('blink_fast', 3)
        with self.mpd_client:
            self.mpd_client.setvol(volume)
            print "volume set to %d" % volume


    def stop(self):
        """On stopping, reset the current playback and stop and clear the playlist

        In contract to pausing, stopping is actually meant to completely stop playing
        the current book and start listening to another"""

        self.playing = False
        self.book.reset()

        self.status_light.action = 'on'

        with self.mpd_client:
            self.mpd_client.stop()
            self.mpd_client.clear()


    def play(self, book_id, progress=None):
        """Play the book as defined in self.book

        1. Get the parts from the current book and add them to the playlsit
        2. Start playing the playlist
        3. Immediately set the position the last know position to resume playback where
           we last left off"""

        def sorter(file1, file2):

            """sorting algorithm for files in playlist"""
            pattern = '(\d+)(-(\d+))?\.mp3'

            try:
                file1_index = re.search(pattern, file1).groups()[2] or 0
                file2_index = re.search(pattern, file2).groups()[2] or 0

                return -1 if int(file1_index) < int(file2_index) else 1
            except:
                return 0


        with self.mpd_client:

            parts = self.mpd_client.search('filename', book_id)

            if not parts:
                self.status_light.interrupt('blink_fast', 3)
                return

            self.mpd_client.clear()

            for part in sorted(parts, cmp=sorter):
                self.mpd_client.add(part['file'])

            self.book.book_id = book_id

            if progress:
                # resume at last known position
                self.book.set_progress(progress)
                self.mpd_client.seek(int(self.book.part) - 1, int(self.book.elapsed))
            else:
                # start playing from the beginning
                self.mpd_client.play()

        self.status_light.action = 'blink'
        self.book.file_info = self.get_file_info()


    def is_playing(self):
        return self.get_status()['state'] == 'play'

    def finished_book(self):
        """return if a book has finished, in which case we need to delete it from the db
        or otherwise we could never listen to that particular book again"""

        status = self.get_status()
        return self.book.book_id is not None and \
               status['state'] == 'stop' and \
               self.book.part == int(status['playlistlength']) and \
               'time' in self.book.file_info and float(self.book.file_info['time']) - self.book.elapsed < 20



    def get_status(self):
        with self.mpd_client:
            return self.mpd_client.status()


    def get_file_info(self):
        with self.mpd_client:
            return self.mpd_client.currentsong()


    def close(self):
        self.stop()
        self.mpd_client.close()
        self.mpd_client.disconnect()
開發者ID:wkjagt,項目名稱:BookPlayer,代碼行數:104,代碼來源:player.py

示例2: Player

# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import set_progress [as 別名]

#.........這裏部分代碼省略.........

        def sorter(file1, file2):

            """sorting algorithm for files in playlist"""
            pattern = "(\d+)(-(.+))?\.mp3"

            try:
                file1_index = re.search(pattern, file1).groups()[1] or 0
                file2_index = re.search(pattern, file2).groups()[1] or 0

                return -1 if int(file1_index) < int(file2_index) else 1
            except:
                return 0

        with self.mpd_client:

            self.set_title_index(book_title)

            parts = self.mpd_client.search("filename", book_title)

            if not parts:
                self.status_light.interrupt("blink_fast", 3)
                return

            self.mpd_client.clear()

            for part in sorted(parts, cmp=sorter):
                self.mpd_client.add(part["file"])

            self.book.book_title = book_title

            if progress:
                # resume at last known position
                self.book.set_progress(progress)
                self.mpd_client.seek(int(self.book.part) - 1, int(self.book.elapsed))
            else:
                # start playing from the beginning
                self.mpd_client.play()

            # print("Now playing: %s %s" % (self.book.book_title, self.book.part))

        self.status_light.action = "blink"
        self.book.file_info = self.get_file_info()

    def get_book_titles(self):
        return self.book_titles

    def first_title(self):
        if len(self.book_titles) == 0:
            self.status_light.interrupt("blink_fast", 3)
            return ""

        with self.index_lock:
            self.current_index = 0

            return self.book_titles[0]

    def next_title(self, channel):
        with self.index_lock:
            if len(self.book_titles) <= self.current_index + 1:
                return self.first_title()

            self.current_index += 1
            return self.book_titles[self.current_index]

    def get_title(self):
開發者ID:grvrulz,項目名稱:BookPlayer,代碼行數:70,代碼來源:player.py


注:本文中的book.Book.set_progress方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。