我正在尝试在 Ubuntu 11.10 笔记本电脑上用 Java 编写自定义合成器。这是我的代码:
package com.sibbo.audio;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
public class Audio {
public static void main(String[] args) throws LineUnavailableException {
byte[] data = new byte[16000 * 2];
sinus(data);
AudioFormat format = new AudioFormat(16000, 8, 1, true, true);
Clip c = javax.sound.sampled.AudioSystem.getClip();
c.open(format, data, 0, data.length); // throws IllegalArgumentException
}
private static void sinus(byte[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (Math.sin(i / 200.0) * 127);
}
}
}
在标记行处,它抛出异常,指出:“格式无效”。如何确定哪些音频格式是合法的?我已经尝试过切换有符号/无符号和小/大端的 boolean 值。
最佳答案
我认为错误消息的意思是相反的。它并不是提示系统不支持 AudioFormat,而是它无法将您的数据解析为这种格式。来自 Javadoc :
Throws:
IllegalArgumentException - if the buffer size does not represent an integral number of sample frames, or if format is not fully specified or invalid
关于java - Java中如何合成声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8280402/