Tutorial

This tutorial explains how to use the ALAudioPlayer module.

Note:

The tutorial is written in Python.

Creating a proxy on the module

Before being able to call some of the ALAudioPlayer methods, you have to create a proxy on the ALAudioPlayer module.

#Creates a proxy on the audio player module from naoqi import ALProxy aup = ALProxy("ALAudioPlayer",IP,9559); #IP = IP address of your robot

Playing a sound file

You can play sounds by using the playFile function. Here is a sample code enabling mp3 and wav files playback.

#Plays a MP3 file aup.playFile("/home/nao/freemp3/feelgood.mp3")

#Plays a WAV file aup.playFile("/opt/naoqi/share/naoqi/wav/0.wav")

You can also play a sound in "non blocking mode" by using the "post" prefix before the call to the playFile function:

#Plays a MP3 file in non blocking mode aup.post.playFile("/home/nao/freemp3/feelgood.mp3")

Playing a sound file with specific volume and audio balance

If you want to play a sound with specific volume and audio balance, you can also gives these parameters to the playFile function. Here is a sample code which plays a mp3 file on the left speaker at a volume of 50%

#Plays a MP3 file on the left speaker to a volume of 50% aup.playFile("/home/nao/freemp3/feelgood.mp3",0.5,-1) # Note: volume can be between 0.0 and 1.0 # audio balance: -1 (left) / 1 (right) / 0 (centered)

Go to a given position during the playback

During the playback, you can go to a given position by using the goTo function. The function needs the id of the current process which is playing the sound. The position must be given in seconds.

#Example: Start the playback of a MP3 file, and go to the 5th second fileId=aup.post.playFile("/home/nao/freemp3/feelgood.mp3") aup.goTo(fileId,5)

Playing a sound file from a given position

The playFileFromPosition method allows to play a sound from a given position (provided as a timestamp in second). Here is a code sample that plays an mp3 file starting from the tenth second.

aup.playFileFromPosition("/home/nao/freemp3/feelgood.mp3",10)

Playing a file in loop

You can play a file in loop with the playFileInLoop method. This method doesn't allow to play the file from a given position, so the playing will start each time at the beginning of the file.

Preload a file and play it

Before playing a file, it is loaded and this takes some time. To reduce the startup time, you can preload it with the loadFile method and play it later. Here is a code sample that does this:

#preloads a file fileId=aup.loadFile("/home/nao/freemp3/feelgood.mp3") # does something else here #and launchs the playing aup.play(fileId)

Stopping the playback

If you are playing several files, you can stop all of them by using the stopAll function.

#Example: launch the playback of two mp3 files and stop them 5 seconds later import time aup.post.playFile("/home/nao/freemp3/mymp31.mp3") aup.post.playFile("/home/nao/freemp3/mymp32.mp3") time.sleep(5) aup.stopAll()

If you are playing several files, you can stop only one of them by using the stop function. You must pass to the function the id of the process which is playing the sound you want to stop.

#Example: launch the playback of two mp3 files and stop the first 5 seconds later import time fileId1 = aup.post.playFile("home/nao/freemp3/mymp31.mp3") fileId2 = aup.post.playFile("home/nao/freemp3/mymp32.mp3") time.sleep(5) aup.stop(fileId1)

Setting the volume of the audio player

You can set either the master volume of the player or the volume for a specific file

If you want to set the master volume, use the setMasterVolume function. The volume can go from 0.0 to 1.0. The current master volume can be obtained by using the getMasterVolume function.

#Example: Adjust the master volume of the audio player to maximum aup.setMasterVolume(1.0)

If you want to set the volume for a specific file, use the setVolume function. The volume can go from 0.0 to 1.0, and you must pass to the function the id of the process which is playing the file to increase or decrease the volume. The current volume can be obtained by using the getVolume function.

#Example: Launches the playing of to mp3 files and adjust the volume for the first one to 50% fileId1 = aup.post.playFile("home/nao/freemp3/mymp31.mp3") fileId2 = aup.post.playFile("home/nao/freemp3/mymp32.mp3") aup.setVolume(fileId1,0.5)





Copyright © 2010 Aldebaran-Robotics - All rights reserved