gutted seek function; addressed initial walk issues
This commit is contained in:
+34
-18
@@ -148,6 +148,8 @@ class NaoMusicPlayer:
|
|||||||
self.current_task_id = None
|
self.current_task_id = None
|
||||||
self.is_playing = False
|
self.is_playing = False
|
||||||
self.paused_position = 0.0
|
self.paused_position = 0.0
|
||||||
|
self.play_start_time = None
|
||||||
|
self.position_at_start = 0.0
|
||||||
self.current_title = ""
|
self.current_title = ""
|
||||||
self.is_loading = False
|
self.is_loading = False
|
||||||
self.load_error = None
|
self.load_error = None
|
||||||
@@ -180,6 +182,12 @@ class NaoMusicPlayer:
|
|||||||
|
|
||||||
_DL_PROGRESS_RE = re.compile(r'\[download\]\s+([\d.]+)%')
|
_DL_PROGRESS_RE = re.compile(r'\[download\]\s+([\d.]+)%')
|
||||||
|
|
||||||
|
def _current_position(self):
|
||||||
|
"""Playback position tracked client-side, independent of NAOqi's getCurrentPosition."""
|
||||||
|
if self.is_playing and self.play_start_time is not None:
|
||||||
|
return self.position_at_start + (time.time() - self.play_start_time)
|
||||||
|
return self.paused_position
|
||||||
|
|
||||||
def play(self, url, title):
|
def play(self, url, title):
|
||||||
self.stop()
|
self.stop()
|
||||||
self.current_title = title
|
self.current_title = title
|
||||||
@@ -252,6 +260,9 @@ class NaoMusicPlayer:
|
|||||||
self.progress = 100.0
|
self.progress = 100.0
|
||||||
self.current_task_id = self.player.playFile(self.remote_path)
|
self.current_task_id = self.player.playFile(self.remote_path)
|
||||||
self.is_playing = True
|
self.is_playing = True
|
||||||
|
self.play_start_time = time.time()
|
||||||
|
self.position_at_start = 0.0
|
||||||
|
self.paused_position = 0.0
|
||||||
print(f"♪ Playing on NAO: {title} (task {self.current_task_id})")
|
print(f"♪ Playing on NAO: {title} (task {self.current_task_id})")
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -265,22 +276,30 @@ class NaoMusicPlayer:
|
|||||||
if self.current_task_id is None or not self.current_file:
|
if self.current_task_id is None or not self.current_file:
|
||||||
print("⏸ Nothing loaded to pause/resume")
|
print("⏸ Nothing loaded to pause/resume")
|
||||||
return
|
return
|
||||||
try:
|
|
||||||
if self.is_playing:
|
if self.is_playing:
|
||||||
pos = self.player.getCurrentPosition(self.current_task_id)
|
pos = self._current_position()
|
||||||
|
try:
|
||||||
self.player.stopAll()
|
self.player.stopAll()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"stopAll error while pausing: {e}")
|
||||||
|
self.load_error = f"Pause error: {e}"
|
||||||
|
return
|
||||||
self.paused_position = max(0.0, pos)
|
self.paused_position = max(0.0, pos)
|
||||||
self.is_playing = False
|
self.is_playing = False
|
||||||
|
self.play_start_time = None
|
||||||
print(f"♪ Paused at {self.paused_position:.1f}s")
|
print(f"♪ Paused at {self.paused_position:.1f}s")
|
||||||
else:
|
else:
|
||||||
|
try:
|
||||||
self.current_task_id = self.player.playFileFromPosition(
|
self.current_task_id = self.player.playFileFromPosition(
|
||||||
self.current_file, self.paused_position, 1.0, 0.0
|
self.current_file, self.paused_position, 1.0, 0.0
|
||||||
)
|
)
|
||||||
self.is_playing = True
|
self.is_playing = True
|
||||||
|
self.play_start_time = time.time()
|
||||||
|
self.position_at_start = self.paused_position
|
||||||
print(f"♪ Resumed from {self.paused_position:.1f}s (task {self.current_task_id})")
|
print(f"♪ Resumed from {self.paused_position:.1f}s (task {self.current_task_id})")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Pause/resume error: {e}")
|
print(f"Resume error: {e}")
|
||||||
self.load_error = f"Pause error: {e}"
|
self.load_error = f"Resume error: {e}"
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
try:
|
try:
|
||||||
@@ -290,16 +309,10 @@ class NaoMusicPlayer:
|
|||||||
self.is_playing = False
|
self.is_playing = False
|
||||||
self.current_file = None
|
self.current_file = None
|
||||||
self.current_task_id = None
|
self.current_task_id = None
|
||||||
|
self.current_title = ""
|
||||||
self.paused_position = 0.0
|
self.paused_position = 0.0
|
||||||
|
self.play_start_time = None
|
||||||
def seek(self, seconds):
|
self.position_at_start = 0.0
|
||||||
if self.current_task_id is None:
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
pos = self.player.getCurrentPosition(self.current_task_id)
|
|
||||||
self.player.goTo(self.current_task_id, max(0.0, pos + seconds))
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Seek error: {e}")
|
|
||||||
|
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
@@ -469,9 +482,16 @@ class NaoTeleop:
|
|||||||
time.sleep(1.0)
|
time.sleep(1.0)
|
||||||
self.motion.setMoveArmsEnabled(True, True)
|
self.motion.setMoveArmsEnabled(True, True)
|
||||||
|
|
||||||
|
print("🦶 Initializing walk engine...")
|
||||||
|
try:
|
||||||
|
self.motion.moveInit()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"moveInit error: {e}")
|
||||||
|
print("🦶 Walk engine ready")
|
||||||
|
|
||||||
carpet_config = [["StepHeight", 0.028], ["MaxStepFrequency", 0.6], ["TorsoWy", 0.05]]
|
carpet_config = [["StepHeight", 0.028], ["MaxStepFrequency", 0.6], ["TorsoWy", 0.05]]
|
||||||
|
|
||||||
print("\n🎮 Controls: WASD/Arrows=Walk | IJKL=Head | SPACE=Reset head | 1=Wave | M=Music search (↑↓ select, Enter confirm) | P=Pause | X=Stop | ,/.=Seek | -/==Volume | T=TTS | ESC=Quit")
|
print("\n🎮 Controls: WASD/Arrows=Walk | IJKL=Head | SPACE=Reset head | 1=Wave | M=Music search (↑↓ select, Enter confirm) | P=Pause | X=Stop | -/==Volume | T=TTS | ESC=Quit")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while self.running:
|
while self.running:
|
||||||
@@ -559,10 +579,6 @@ class NaoTeleop:
|
|||||||
self.music.pause()
|
self.music.pause()
|
||||||
elif event.key == pygame.K_x:
|
elif event.key == pygame.K_x:
|
||||||
self.music.stop()
|
self.music.stop()
|
||||||
elif event.key == pygame.K_COMMA:
|
|
||||||
self.music.seek(-10)
|
|
||||||
elif event.key == pygame.K_PERIOD:
|
|
||||||
self.music.seek(10)
|
|
||||||
elif event.key in (pygame.K_MINUS, pygame.K_KP_MINUS):
|
elif event.key in (pygame.K_MINUS, pygame.K_KP_MINUS):
|
||||||
self.change_volume(-5)
|
self.change_volume(-5)
|
||||||
elif event.key in (pygame.K_EQUALS, pygame.K_KP_PLUS):
|
elif event.key in (pygame.K_EQUALS, pygame.K_KP_PLUS):
|
||||||
|
|||||||
Reference in New Issue
Block a user