fix: current_index unchanged immediately after stop

This commit is contained in:
Terry Geng 2020-03-25 21:34:31 +08:00
parent 732c698408
commit 1732314304
3 changed files with 18 additions and 15 deletions

View File

@ -681,7 +681,11 @@ def cmd_current_music(bot, user, text, command, parameter):
def cmd_skip(bot, user, text, command, parameter): def cmd_skip(bot, user, text, command, parameter):
global log global log
bot.interrupt() if not bot.is_pause:
bot.interrupt()
else:
var.playlist.next()
bot.wait_for_ready = True
if len(var.playlist) == 0: if len(var.playlist) == 0:
bot.send_msg(constants.strings('queue_empty'), text) bot.send_msg(constants.strings('queue_empty'), text)

View File

@ -182,7 +182,7 @@ class CachedItemWrapper:
def format_song_string(self): def format_song_string(self):
return self.item().format_song_string(self.user) return self.item().format_song_string(self.user)
def format_short_string(self): def format_title(self):
return self.item().format_title() return self.item().format_title()
def format_debug_string(self): def format_debug_string(self):

View File

@ -70,7 +70,7 @@ class MumbleBot:
self.last_ffmpeg_err = "" self.last_ffmpeg_err = ""
self.read_pcm_size = 0 self.read_pcm_size = 0
# self.download_threads = [] # self.download_threads = []
self.wait_for_downloading = False # flag for the loop are waiting for download to complete in the other thread self.wait_for_ready = False # flag for the loop are waiting for download to complete in the other thread
if var.config.getboolean("webinterface", "enabled"): if var.config.getboolean("webinterface", "enabled"):
wi_addr = var.config.get("webinterface", "listening_addr") wi_addr = var.config.get("webinterface", "listening_addr")
@ -351,7 +351,7 @@ class MumbleBot:
def launch_music(self): def launch_music(self):
if var.playlist.is_empty(): if var.playlist.is_empty():
return return
assert self.wait_for_downloading is False assert self.wait_for_ready is False
music_wrapper = var.playlist.current_item() music_wrapper = var.playlist.current_item()
uri = music_wrapper.uri() uri = music_wrapper.uri()
@ -455,37 +455,33 @@ class MumbleBot:
var.cache.free_and_delete(current.id) var.cache.free_and_delete(current.id)
# move to the next song. # move to the next song.
if not self.wait_for_downloading: if not self.wait_for_ready: # if wait_for_ready flag is not true, move to the next song.
if var.playlist.next(): if var.playlist.next():
current = var.playlist.current_item() current = var.playlist.current_item()
if current.validate(): if current.validate():
if current.is_ready(): if not current.is_ready():
self.launch_music()
self.async_download_next()
else:
self.log.info("bot: current music isn't ready, start downloading.") self.log.info("bot: current music isn't ready, start downloading.")
self.wait_for_downloading = True
var.playlist.async_prepare(var.playlist.current_index) var.playlist.async_prepare(var.playlist.current_index)
self.send_msg(constants.strings('download_in_progress', item=current.format_title())) self.send_msg(constants.strings('download_in_progress', item=current.format_title()))
self.wait_for_ready = True
else: else:
var.playlist.remove_by_id(current.id) var.playlist.remove_by_id(current.id)
var.cache.free_and_delete(current.id) var.cache.free_and_delete(current.id)
else: else:
self._loop_status = 'Empty queue' self._loop_status = 'Empty queue'
else: else: # if wait_for_ready flag is true, means the pointer is already pointing to target song. start playing
current = var.playlist.current_item() current = var.playlist.current_item()
if current: if current:
if current.is_ready(): if current.is_ready():
self.wait_for_downloading = False self.wait_for_ready = False
var.playlist.version += 1
self.launch_music() self.launch_music()
self.async_download_next() self.async_download_next()
elif current.is_failed(): elif current.is_failed():
var.playlist.remove_by_id(current.id) var.playlist.remove_by_id(current.id)
else: else:
self._loop_status = 'Wait for downloading' self._loop_status = 'Wait for the next item to be ready'
else: else:
self.wait_for_downloading = False self.wait_for_ready = False
while self.mumble.sound_output.get_buffer_size() > 0: while self.mumble.sound_output.get_buffer_size() > 0:
# Empty the buffer before exit # Empty the buffer before exit
@ -540,11 +536,14 @@ class MumbleBot:
self.thread.kill() self.thread.kill()
self.thread = None self.thread = None
var.playlist.clear() var.playlist.clear()
self.wait_for_ready = False
self.log.info("bot: music stopped. playlist trashed.") self.log.info("bot: music stopped. playlist trashed.")
def stop(self): def stop(self):
self.interrupt() self.interrupt()
self.is_pause = True self.is_pause = True
var.playlist.next()
self.wait_for_ready = True
self.log.info("bot: music stopped.") self.log.info("bot: music stopped.")
def interrupt(self): def interrupt(self):