0
$\begingroup$

Rosanswers logo

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.


Originally posted by DocSmiley on ROS Answers with karma: 127 on 2012-08-14

Post score: 2


Original comments

Comment by dinosaur on 2015-09-21:
Here's the similar question about text-to-speech: http://answers.ros.org/question/10829/text-to-speech-in-a-python-node/

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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()

Originally posted by pgrice with karma: 196 on 2012-08-15

This answer was ACCEPTED on the original site

Post score: 3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.