本文整理汇总了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"""
#.........这里部分代码省略.........
示例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()
#.........这里部分代码省略.........