forked from ConorPNorris/summer-2024-final-project-team-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_player_node.py
35 lines (29 loc) · 1009 Bytes
/
audio_player_node.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import rclpy
from rclpy.node import Node
from std_msgs.msg import String
import subprocess
class AudioPlayerNode(Node):
def __init__(self):
super().__init__('audio_player_node')
self.subscription = self.create_subscription(
String,
'play_audio',
self.listener_callback,
10)
self.get_logger().info('Audio Player Node is ready.')
def listener_callback(self, msg):
audio_file = msg.data
self.get_logger().info(f'Playing audio: {audio_file}')
try:
# Use subprocess to call the sox play command
subprocess.run(["play", audio_file], check=True)
except subprocess.CalledProcessError as e:
self.get_logger().error(f"Failed to play sound: {e}")
def main(args=None):
rclpy.init(args=args)
audio_player_node = AudioPlayerNode()
rclpy.spin(audio_player_node)
audio_player_node.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()