Skip to content

Commit

Permalink
put music playing in background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
CullenSUN committed Nov 11, 2023
1 parent 7782694 commit a097757
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion mini_pupper_music/mini_pupper_music/music_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from rclpy.node import Node
from mini_pupper_interfaces.srv import MusicCommand
from playsound import playsound
import threading
import os
from ament_index_python.packages import get_package_share_directory

Expand Down Expand Up @@ -53,7 +54,14 @@ def play_sound_file(self, file_name):
package_name = 'mini_pupper_music'
package_path = get_package_share_directory(package_name)
sound_path = os.path.join(package_path, 'resource', file_name)
playsound(sound_path, block=False)

# Create a new thread for playing the sound
thread = threading.Thread(target=self.play_sound_in_background, args=(sound_path,))
thread.daemon = True # Set the thread as a daemon (will exit when the main program ends)
thread.start()

def play_sound_in_background(self, sound_path):
playsound(sound_path)


def main(args=None):
Expand Down

0 comments on commit a097757

Please sign in to comment.