ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Play a sound file in a python node

asked 2012-08-14 06:57:39 -0500

DocSmiley gravatar image

updated 2014-01-28 17:13:20 -0500

ngrennan gravatar image

I've done the soundplay tutorial and can play a file from the command line, but can someone explain how to do it from inside a python program?

I've followed the directions from a similar question about the text-to-speech and that works fine but I'm unsure what changes are needed for a file instead.

edit retag flag offensive close merge delete


Here's the similar question about text-to-speech:

dinosaur gravatar image dinosaur  ( 2015-09-21 23:05:02 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2012-08-15 06:04:51 -0500

pgrice gravatar image

updated 2012-08-15 06:06:37 -0500

Similar to using the text-to-speech from the sound_play node, you have two (very similar) options. First import the SoundClient from the sound_play.libsoundplay. Then you can play a sound file (.wav or .ogg) with the sound client's '.playWave("/full/path/to/sound.wav")' method. Or you can use the client's '.waveSound("/full/path/to/sound.wav")' method to create a Sound object. Then use the Sound objects '.play()' method to play the sound.

Here is an example snippet showing both:

    #!usr/bin/env python
    import roslib; roslib.load_manifest('sound_play')
    import rospy
    from sound_play.libsoundplay import SoundClient

    #Create a sound client instance
    sound_client = SoundClient()
    #wait for sound_play node to connect to publishers (otherwise it will miss first published msg)
    #Method 1: Play Wave file directly from Client

    #Method 2: Create Sound instance for file, play with '.play()' method
    sound = sound_client.waveSound('/full/path/to/sound.wav')
edit flag offensive delete link more

Question Tools


Asked: 2012-08-14 06:57:39 -0500

Seen: 2,642 times

Last updated: Aug 15 '12