现在回过头来看sayPhoneWord(),这里还有一个方法我们没有分析,即getSound()方法。
getSound()方法从一个au文件以字节数据的形式读入预先录制的声音样本。要了解读取数据、转换音频格式、初始化声音输出行(SouceDataLine)以及构造字节数据的详细过程,请参考下面代码中的注释:
/*
* 该方法从文件读取一个音素,
* 并把它转换成byte数组
*/
private byte[] getSound(String fileName)
{
try
{
URL url=Talker.class.getResource(fileName);
AudioInputStream stream = AudioSystem.getAudioInputStream(url);
AudioFormat format = stream.getFormat();
// 把一个ALAW/ULAW声音转换成PCM以便回放
if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
(format.getEncoding() == AudioFormat.Encoding.ALAW))
{
AudioFormat tmpFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(), format.getSampleSizeInBits() * 2,
format.getChannels(), format.