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

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

Comments

Here's the similar question about text-to-speech: http://answers.ros.org/question/10829...

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

1 Answer

Sort by ยป oldest newest most voted
3

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

    rospy.init_node('play_sound_file')
    #Create a sound client instance
    sound_client = SoundClient()
    #wait for sound_play node to connect to publishers (otherwise it will miss first published msg)
    rospy.sleep(2)
    #Method 1: Play Wave file directly from Client
    sound_client.playWave('/full/path/to/sound.wav')

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

Question Tools

Stats

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

Seen: 2,609 times

Last updated: Aug 15 '12