Cannot be tested on a simulated robot.
In this tutorial you will see, over an example, how to:
Create the following box: a Script box playing a music from a MP3 file.
Step | Action |
---|---|
In the Project content panel, click Plus button then choose Import files. | |
Select the file to attach. The file is displayed in the project content, and will be sent on the robot with the behavior. |
Now we are going to create a simple box that will play a music in order to use an attached file as parameter.
Step | Action |
---|---|
Create a new Python box. For further details, see: How to create a Python box. |
|
Add a parameter to the box, and specify “File name” as Name, and “Attached file” as Type. For further details, see: How to add/remove inputs, outputs, parameters in a box. |
|
Click the Parameter button, to set the value of the parameter. |
|
Click the browse button and select one of the files available in the Project content. Note that if you had not yet imported the file in the Project content, you can do it prior to select the file. |
Now we will edit the script of the box in order to read the music.
Step | Action |
---|---|
Double click the box to display the Script editor panel. For further details, see: Script editor panel. As you can see, the structure of the code were automatically generated. |
|
In the onLoad method, create a proxy to AlAudioPlayer module. For further deails, see: ALAudioPlayer. def onLoad(self):
self.player = ALProxy('ALAudioPlayer')
|
|
In the onStart method, call the method playFile() with the path to the attached file as parameter. The attached file will be send to the robot when you will start the behavior, so the path has to lead to the attached file on the robot. os.path.join(self.behaviorAbsolutePath(), self.getParameter("File name"))
It is a concatenation of the behavior path on the robot and the file name. |
|
In the onUnload method, call the method stopAll(). The code of the box should look like that: class MyClass(GeneratedClass):
def __init__(self):
GeneratedClass.__init__(self)
def onLoad(self):
self.player = ALProxy('ALAudioPlayer')
def onUnload(self):
self.player.stopAll()
def onInput_onStart(self):
self.player.playFile(os.path.join(self.behaviorAbsolutePath(), self.getParameter("File name")))
def onInput_onStop(self):
self.onUnload()
|
Testing
Now, let’s test it:
Step | Action |
---|---|
In your root diagram, link the onStart input of the box to the beginning of the behavior. | |
Click the Play button to start the behavior. Your robot is playing the music given as an attached file. |