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


Python Book.reset方法代碼示例

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


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

示例1: Player

# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import reset [as 別名]
class Player(object):

    """The class responsible for playing the audio books"""

    def __init__(self, conn_details, status_light):
        """Setup a connection to MPD to be able to play audio.

        Also update the MPD database with any new MP3 files that may have been added
        and clear any existing playlists.
        """
        self.status_light = status_light
        self.book = Book()

        self.mpd_client = LockableMPDClient()
        self.init_mpd(conn_details)

    def init_mpd(self, conn_details):
        try:
            print "Connecting to MPD."
            with self.mpd_client:
                self.mpd_client.connect(**conn_details)

                self.mpd_client.update()
                self.mpd_client.clear()
                self.mpd_client.setvol(100)
        except:
            print "Connection to MPD failed. Trying again in 10 seconds."
            time.sleep(10)
            self.init_mpd(conn_details)

    def toggle_pause(self, channel):
        """Toggle playback status between play and pause"""

        with self.mpd_client:
            state = self.mpd_client.status()['state']
            if state == 'play':
                self.status_light.action = 'blink_pauze'
                self.mpd_client.pause()
            elif state == 'pause':
                self.status_light.action = 'blink'
                self.mpd_client.play()
            else:
                self.status_light.interrupt('blink_fast', 3)

    def rewind(self, channel):
        """Rewind by 20s"""

        self.status_light.interrupt('blink_fast', 3)

        if self.is_playing():
            song_index = int(self.book.part) - 1
            elapsed = int(self.book.elapsed)

            with self.mpd_client:

                if elapsed > 20:
                    # rewind withing current file if possible
                    self.mpd_client.seek(song_index, elapsed - 20)
                elif song_index > 0:
                    # rewind to previous file if we're not yet 20 seconds into
                    # the current file
                    prev_song = self.mpd_client.playlistinfo(song_index - 1)[0]
                    prev_song_len = int(prev_song['time'])

                    # if the previous part is longer than 20 seconds, rewind to 20
                    # seconds before the end, otherwise rewind to the start of it
                    if prev_song_len > 20:
                        self.mpd_client.seek(song_index - 1, prev_song_len - 20)
                    else:
                        self.mpd_client.seek(song_index - 1, 0)
                else:
                    # if we're less than 20 seconds into the first part, rewind
                    # to the start of it
                    self.mpd_client.seek(0, 0)


    def volume_up(self, channel):
        volume = int(self.get_status()['volume'])
        self.set_volume(min(volume + 10, 100))


    def volume_down(self, channel):

        volume = int(self.get_status()['volume'])
        self.set_volume(max(volume - 10, 0))


    def set_volume(self, volume):
        """Set the volume on the MPD client"""
        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"""
#.........這裏部分代碼省略.........
開發者ID:wkjagt,項目名稱:BookPlayer,代碼行數:103,代碼來源:player.py

示例2: Player

# 需要導入模塊: from book import Book [as 別名]
# 或者: from book.Book import reset [as 別名]
class Player(object):

    """The class responsible for playing the audio books"""

    def __init__(self, conn_details, status_light):

        """Setup a connection to MPD to be able to play audio.
        Also update the MPD database with any new MP3 files that may have been added
        and clear any existing playlists.
        """
        self.status_light = status_light
        self.book = Book()
        self.book_titles = []

        self.index_lock = RLock()
        self.current_index = -1

        self.mpd_client = LockableMPDClient()
        with self.mpd_client:
            self.mpd_client.connect(**conn_details)

            self.mpd_client.update()
            self.mpd_client.clear()
            self.mpd_client.setvol(100)

            files = self.mpd_client.search("filename", ".mp3")
            if not files:
                self.status_light.interrupt("blink_fast", 3)
            else:
                for file in files:
                    book_title = os.path.dirname(file["file"]) + "/"
                    if book_title not in self.book_titles:
                        self.book_titles.append(book_title)

    def toggle_pause(self, channel):
        """Toggle playback status between play and pause"""

        with self.mpd_client:
            state = self.mpd_client.status()["state"]
            if state == "play":
                self.status_light.action = "blink_pauze"
                self.mpd_client.pause()
            elif state == "pause":
                self.status_light.action = "blink"
                self.mpd_client.play()
            else:
                self.status_light.interrupt("blink_fast", 3)

    def rewind(self, channel):
        """Rewind by 30s"""
        self.status_light.interrupt("blink_fast", 3)
        if self.is_playing():
            song_index = int(self.book.part) - 1
            elapsed = int(self.book.elapsed)

            with self.mpd_client:

                if elapsed > 30:
                    # rewind withing current file if possible
                    self.mpd_client.seek(song_index, elapsed - 30)
                elif song_index > 0:
                    # rewind to previous file if we're not yet 30 seconds into
                    # the current file
                    prev_song = self.mpd_client.playlistinfo(song_index - 1)[0]
                    prev_song_len = int(prev_song["time"])

                    # if the previous part is longer than 30 seconds, rewind to 30
                    # seconds before the end, otherwise rewind to the start of it
                    if prev_song_len > 30:
                        self.mpd_client.seek(song_index - 1, prev_song_len - 30)
                    else:
                        self.mpd_client.seek(song_index - 1, 0)
                else:
                    # if we're less than 30 seconds into the first part, rewind
                    # to the start of it
                    self.mpd_client.seek(0, 0)

    def volume_up(self, channel):
        volume = int(self.get_status()["volume"])
        self.set_volume(min(volume + 5, 100))

    def volume_down(self, channel):

        volume = int(self.get_status()["volume"])
        self.set_volume(max(volume - 5, 0))

    def set_volume(self, volume):
        """Set the volume on the MPD client"""
        self.status_light.interrupt("blink_fast", 3)
        with self.mpd_client:
            self.mpd_client.setvol(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()
#.........這裏部分代碼省略.........
開發者ID:grvrulz,項目名稱:BookPlayer,代碼行數:103,代碼來源:player.py


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