Skip to content

Commit 28d2d56

Browse files
committed
🐛 fix lyric show bug
1 parent dbb26c8 commit 28d2d56

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

SpotifyLyricWindow/common/player/lyric_player.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def get_time(self) -> int:
3939
def set_pause(self, flag: bool):
4040
"""设置歌词播放是否暂停"""
4141
self.is_pause = flag
42+
if not flag:
43+
self.thread_play_lrc.reset_position()
4244

4345
def set_track(self, track_id: str, duration: int):
4446
"""设置当前播放的歌曲 duration单位:ms"""
@@ -54,7 +56,7 @@ def set_track(self, track_id: str, duration: int):
5456
self.lrc_file = LrcFile()
5557

5658
if self.trans_mode not in self.lrc_file.available_trans():
57-
self.set_trans_mode(TransType.NON)
59+
self.trans_mode = TransType.NON
5860

5961
def set_trans_mode(self, mode: TransType) -> bool:
6062
"""设置翻译模式"""
@@ -79,11 +81,12 @@ def _show_last_lyric(self):
7981
if order != -1:
8082
self.show_content(order, 0)
8183

82-
def seek_to_position(self, position: int):
84+
def seek_to_position(self, position: int, is_show_last_lyric=True):
8385
"""调整歌词播放进度 position单位:ms"""
8486
self.timer_start_value = int(time.time() * 1000) - position
85-
self._show_last_lyric()
86-
self.thread_play_lrc.reset_position()
87+
if is_show_last_lyric:
88+
self._show_last_lyric()
89+
self.thread_play_lrc.reset_position()
8790

8891
def modify_offset(self, modify_value: int):
8992
"""修改单个歌词文件的偏移"""
@@ -133,24 +136,25 @@ def _play_lyric_thread(self):
133136
position = self.player.get_time()
134137
lyric_order = self.player.lrc_file.get_order_position(position)
135138
next_stamp = self.player.lrc_file.get_time(lyric_order + 1)
136-
137-
if position > self.player.duration - 1100: # 播放完毕
139+
if position > self.player.duration - 1200: # 播放完毕
138140
if self.player.play_done_event_func and not self.player.is_pause:
139141
self.player.play_done_event_func() # 发送播放完毕信号
140142
self.sleep.wait()
141143
continue
142144
if lyric_order < 0: # 无歌词
143145
self.sleep.wait(0.1) # 开始检测何时播放完毕
144146
continue
145-
elif next_stamp < 2:
147+
elif next_stamp < 0:
146148
next_stamp = self.player.duration
147149

148150
show_time = next_stamp - position
149151

150152
if show_time < 0: # 位置判断变化导致时间错误 重新获取
151153
continue
152154
self.player.show_content(lyric_order, show_time)
153-
self.sleep.wait(show_time / 1000)
155+
156+
if next_stamp != self.player.duration: # 播放最后一句时不需要等待
157+
self.sleep.wait(show_time / 1000)
154158

155159
if self.player.is_pause and self.is_running:
156160
self.sleep.wait()

SpotifyLyricWindow/components/lyric_window.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __init__(self, parent=None):
6464
self._init_signal()
6565
self._init_media_session()
6666

67-
self.calibration_event()
67+
self.calibration_event(use_api_position=True)
6868

6969
def _re_init(self):
7070
"""重新初始化"""
@@ -124,21 +124,26 @@ def media_properties_changed(self, info: MediaPropertiesInfo):
124124

125125
track_title = f"{info.title} - {info.artist}"
126126
track_id = self.lyric_file_manage.get_id(track_title)
127-
128-
if not self._manual_skip_flag:
129-
# 自动切换到下一首歌 将会有将近700ms的歌曲准备时间导致时间定位不准确
130-
time.sleep(0.7)
131-
self.media_session.seek_to_position_media(0)
132-
self._manual_skip_flag = True
133-
else:
134-
# 手动切换到下一首歌 可能会有延迟也可能没有,故不做处理
135-
time.sleep(0.5) # 等待api反应过来
136-
137127
if track_id and not self.lyric_file_manage.get_not_found(track_id): # 如果可以通过title直接获取id, 则不走api渠道
138128
playback_info = self.media_session.get_current_playback_info()
139-
self.playback_info_changed(playback_info)
140129
self.lrc_player.set_track(track_id, playback_info.duration)
130+
if not self._manual_skip_flag:
131+
# 自动切换到下一首歌 将会有将近700ms的歌曲准备时间导致时间定位不准确
132+
time.sleep(0.7)
133+
self.media_session.seek_to_position_media(0)
134+
self.lrc_player.seek_to_position(0)
135+
self._manual_skip_flag = True
136+
self.lrc_player.set_pause(not (playback_info.playStatus == 4))
141137
else:
138+
if not self._manual_skip_flag:
139+
# 自动切换到下一首歌 将会有将近700ms的歌曲准备时间导致时间定位不准确
140+
time.sleep(0.7)
141+
self.media_session.seek_to_position_media(0)
142+
self.lrc_player.seek_to_position(0)
143+
self._manual_skip_flag = True
144+
else:
145+
# 手动切换到下一首歌 可能会有延迟也可能没有,故不做处理
146+
time.sleep(0.5) # 等待api反应过来
142147
self.calibration_event()
143148

144149
def playback_info_changed(self, info: MediaPlaybackInfo):
@@ -149,18 +154,22 @@ def playback_info_changed(self, info: MediaPlaybackInfo):
149154
self.set_lyrics_rolling(is_playing)
150155

151156
def timeline_properties_changed(self, info: MediaPlaybackInfo):
157+
if not self._manual_skip_flag:
158+
return
152159
if info.position < 500 or self.lrc_player.is_pause:
153160
# 由于切换到下一首歌会同时触发timeline和properties的变化信号,利用position<500过滤掉切换歌时候的timeline信号
161+
self.lrc_player.seek_to_position(info.position, is_show_last_lyric=False)
154162
return
155163
self.lrc_player.seek_to_position(info.position)
156164

157165
@thread_drive()
158166
@CatchError
159-
def calibration_event(self, *_, no_text_show: bool = False):
167+
def calibration_event(self, *_, no_text_show: bool = False, use_api_position: bool = False):
160168
"""
161169
同步歌词
162170
163171
:param no_text_show: 是否显示 calibrating! 的 正在校准提示
172+
:param use_api_position: 是否使用api的时间进行校准
164173
"""
165174
if not no_text_show:
166175
self.set_lyrics_text(1, "calibrating!")
@@ -194,12 +203,9 @@ def calibration_event(self, *_, no_text_show: bool = False):
194203
self.lyric_file_manage.set_not_found(user_current.track_id, "") # 将 不存在 记录撤去
195204
user_current = self._refresh_player_track(user_current)
196205

197-
ava_trans = self.lrc_player.lrc_file.available_trans()
198-
199206
self.lrc_player.set_pause(not user_current.is_playing)
200-
self.lrc_player.set_trans_mode(self.user_trans if self.user_trans in ava_trans else TransType.NON)
201207

202-
if not self.media_session.is_connected():
208+
if not self.media_session.is_connected() or use_api_position:
203209
self.lrc_player.seek_to_position(user_current.progress_ms)
204210

205211
@thread_drive()

0 commit comments

Comments
 (0)