程序说明书索引 >> 红色说明书索引

>音频系统

引言

ALAudio Player (音频播放器)

ALTextToSpeech (声音合成)

ALSpeechRecognition (语音识别)

ALAudioDevice (音频器件)

ALSoundDetection(声音识别)

ALAudioSourceLocalization(音频源定位)

使用指导

本指导介绍如何在一个程序里使用ALAudioPlayer。

注释:

本指导使用Python语言。

在模块上创建一个代理(proxy)。

在调用ALAudioPlayer方法之前,必须首先创建一个ALAudioPlayer模块的代理。

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

播放声音文件

可以使用playFile函数来播放声音。以下是一个代码示例,用来启用MP3和WAV文件的回放。

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

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

还可以在“非阻塞模式”下播放声音文件,只需在调用的playFile函数前加前缀“post”:

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

使用特定的音量和音频平衡来播放一个声音文件

如果您想使用特定的音量和音频平衡,您也可以把这些参数在playFile函数中给出。以下的代码示例是在左侧的扬声器中以50%的音量播放一个MP3文件:

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

在回放过程中转入指定位置

在回放过程中,可以通过goTo函数,转入一个指定的位置。 必须把正播放声音的当前进程ID传递给该函数。 位置必须以秒的形式给出。

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

从一个指定位置播放声音文件

通过playFileFromPosition方法,可以从一个指定的位置(以秒为单位的时间戳)播放声音。 以下是一个代码示例,从第10秒开始播放MP3文件:

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

在回路中播放一个文件

您可以使用playFileInLoop方法在回路中播放一个文件。该方法无法在某一特定位置播放文件,因此每次播放文件时都会从头开始。

预加载一个文件并播放

在播放一个文件之前,需要加载这个文件。这个过程需要一定的时间。为了缩短启动时间,您可以使用loadFile方法预加载这个文件,供稍后播放。 以下是该操作的一个代码示例:

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

停止回放

如要停止所有正在播放的文件,可以使用stopAll函数。

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

如要停止正在播放的若干文件中的一个,可以使用stop函数。 必须把正在播放相应文件的进程ID传递给该函数。

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

设定音频播放器的音量

您可以设定播放器的主音量,或是为一个特定的文件设定音量。

如果您要设定主音量,请使用setMasterVolume函数。音量可设定在0.0到1.0之间。 通过函输getMasterVolume可以获得当前主音量。

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

I如果您要为一个特定文件设定音量,请使用setVolume函数。音量可设定在0.0到1.0之间,而且您必须把播放文件的进程ID在函数中给出,来提高或降低音量。 通过getVolume函数可以获得当前音量。

#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 - 版权所有