work on music player
This commit is contained in:
+17
-10
@@ -11,6 +11,7 @@ import cv2
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import pygame
|
import pygame
|
||||||
import yt_dlp
|
import yt_dlp
|
||||||
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
|
||||||
# --- Audio for mic ---
|
# --- Audio for mic ---
|
||||||
@@ -128,15 +129,17 @@ class SoundReceiver:
|
|||||||
|
|
||||||
|
|
||||||
# ==========================================
|
# ==========================================
|
||||||
# MUSIC PLAYER ON NAO
|
# MUSIC PLAYER ON NAO (scp)
|
||||||
# ==========================================
|
# ==========================================
|
||||||
class NaoMusicPlayer:
|
class NaoMusicPlayer:
|
||||||
def __init__(self, session):
|
def __init__(self, session, nao_ip):
|
||||||
self.session = session
|
self.session = session
|
||||||
self.player = session.service("ALAudioPlayer")
|
self.player = session.service("ALAudioPlayer")
|
||||||
|
self.nao_ip = nao_ip
|
||||||
self.music_dir = "/home/nao/music"
|
self.music_dir = "/home/nao/music"
|
||||||
try:
|
try:
|
||||||
self.session.service("ALFileManager").createFolder(self.music_dir)
|
subprocess.run(["sshpass", "-p", "nao", "ssh", f"nao@{nao_ip}", f"mkdir -p {self.music_dir}"],
|
||||||
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||||
except: pass
|
except: pass
|
||||||
self.current_file = None
|
self.current_file = None
|
||||||
self.is_playing = False
|
self.is_playing = False
|
||||||
@@ -173,13 +176,16 @@ class NaoMusicPlayer:
|
|||||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||||
info = ydl.extract_info(url, download=True)
|
info = ydl.extract_info(url, download=True)
|
||||||
if not info:
|
if not info:
|
||||||
|
print("Download failed")
|
||||||
return False
|
return False
|
||||||
downloaded = ydl.prepare_filename(info)
|
downloaded = ydl.prepare_filename(info)
|
||||||
|
|
||||||
remote_path = f"{self.music_dir}/{int(time.time())}.mp3"
|
remote_path = f"{self.music_dir}/{int(time.time())}.mp3"
|
||||||
file_manager = self.session.service("ALFileManager")
|
result = subprocess.run(["sshpass", "-p", "nao", "scp", downloaded, f"nao@{self.nao_ip}:{remote_path}"],
|
||||||
# Use uploadFile instead of put
|
capture_output=True, text=True)
|
||||||
file_manager.uploadFile(downloaded, remote_path)
|
if result.returncode != 0:
|
||||||
|
print(f"SCP failed: {result.stderr}")
|
||||||
|
return False
|
||||||
|
|
||||||
self.current_file = remote_path
|
self.current_file = remote_path
|
||||||
self.player.playFile(remote_path)
|
self.player.playFile(remote_path)
|
||||||
@@ -218,8 +224,9 @@ class NaoMusicPlayer:
|
|||||||
# MAIN TELEOP
|
# MAIN TELEOP
|
||||||
# ==========================================
|
# ==========================================
|
||||||
class NaoTeleop:
|
class NaoTeleop:
|
||||||
def __init__(self, session, audio_device_index=None, mic_channel=1, mic_gain=8.0, video_scale=2.0):
|
def __init__(self, session, nao_ip, audio_device_index=None, mic_channel=1, mic_gain=8.0, video_scale=2.0):
|
||||||
self.session = session
|
self.session = session
|
||||||
|
self.nao_ip = nao_ip
|
||||||
self.motion = session.service("ALMotion")
|
self.motion = session.service("ALMotion")
|
||||||
self.posture = session.service("ALRobotPosture")
|
self.posture = session.service("ALRobotPosture")
|
||||||
self.tts = session.service("ALTextToSpeech")
|
self.tts = session.service("ALTextToSpeech")
|
||||||
@@ -228,7 +235,7 @@ class NaoTeleop:
|
|||||||
except:
|
except:
|
||||||
self.battery = None
|
self.battery = None
|
||||||
|
|
||||||
self.music = NaoMusicPlayer(session)
|
self.music = NaoMusicPlayer(session, nao_ip)
|
||||||
self.music_results = []
|
self.music_results = []
|
||||||
self.selected_result = 0
|
self.selected_result = 0
|
||||||
|
|
||||||
@@ -432,7 +439,7 @@ class NaoTeleop:
|
|||||||
self.music_results = []
|
self.music_results = []
|
||||||
elif event.key == pygame.K_p:
|
elif event.key == pygame.K_p:
|
||||||
self.music.pause()
|
self.music.pause()
|
||||||
elif event.key == pygame.K_x: # Stop music
|
elif event.key == pygame.K_x:
|
||||||
self.music.stop()
|
self.music.stop()
|
||||||
elif event.key == pygame.K_LEFT:
|
elif event.key == pygame.K_LEFT:
|
||||||
self.music.seek(-10)
|
self.music.seek(-10)
|
||||||
@@ -538,7 +545,7 @@ if __name__ == "__main__":
|
|||||||
print(f"❌ Connection failed: {e}")
|
print(f"❌ Connection failed: {e}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
NaoTeleop(session,
|
NaoTeleop(session, nao_ip=args.ip,
|
||||||
audio_device_index=args.audio_device_index,
|
audio_device_index=args.audio_device_index,
|
||||||
mic_channel=MIC_CHANNELS[args.mic_channel],
|
mic_channel=MIC_CHANNELS[args.mic_channel],
|
||||||
mic_gain=args.mic_gain,
|
mic_gain=args.mic_gain,
|
||||||
|
|||||||
Reference in New Issue
Block a user