我正在开发一个项目,该项目将根据输出声级通过 PWM 信号改变键盘灯的亮度。那个麻烦是,我需要实时的输出流。到目前为止,我使用了 javax.sound.sampled 包,但从未成功获取音频输出。但是,我所做的是使用 targetDataLine 和 sourceDataLine,这看起来不像要走的路,但我仍在尝试。我需要的是一个音频流提供程序,我的 java 应用程序可以“听到”并处理它。到目前为止,我查看了许多教程、视频、帖子、一些文章(例如:https ://cr.openjdk.java.net/~iris/se/12/latestSpec/api/java.desktop/javax/sound/采样/类使用/Mixer.Info.html) 等,但没有结果。以前有人做过吗?或者除了采样库之外还有其他图书馆吗?任何帮助,将不胜感激。
我得到的每种格式的错误:
java.lang.IllegalArgumentException: Line unsupported: interface TargetDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
at com.sun.media.sound.DirectAudioDevice.getLine(Unknown Source)
at javax.sound.sampled.AudioSystem.getTargetDataLine(Unknown Source)
使用的代码:
package audio;
import javax.sound.sampled.*;
public class App1 {
public static String getWord(String arr) {
int i = 0;
for(; i < arr.length(); i++) {
if (arr.charAt(i) == ' ')
break;
}
return arr.substring(0,i);
}
public static void wait(int ms) {
try{
Thread.sleep(ms);
}
catch(InterruptedException ex){
Thread.currentThread().interrupt();
}
}
public static AudioFormat getAudioFormat(){
float sampleRate = 44100;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 1;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
//return new AudioFormat(Encoding.PCM_SIGNED, sampleRate, 16, 1, 2, sampleRate, false);
return new AudioFormat(sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String [] args) {
try {
//-----DECLARATIONS-----
TargetDataLine targetDataLine = null;
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
Mixer.Info m = null;
String expectedMixer = "Speakers";
//-----MIXER FINDER-----
System.out.println("Number of mixers: " + mixers.length);
for(int i = 0; i < mixers.length; i++) {
System.out.println(getWord(mixers[i].getName()));
if(getWord(mixers[i].getName()).compareTo(expectedMixer) == 0){
m = mixers[i];
}
}
if(m==null) throw new Exception("No such mixer found: " + expectedMixer);
else System.out.println('\n'+"Device choosen: "+m.getName());
//-----LINE TESTER-----
boolean v = false, showError = true; // show error or keep trying several times
int tries = 3, i = 0;
while(v==false && i++ < tries){
try {
//sourceDataLine = AudioSystem.getTargetDataLine(getAudioFormat(), m);
targetDataLine = AudioSystem.getTargetDataLine(getAudioFormat(), m);
targetDataLine.open(getAudioFormat());
v=true;
//System.out.println("Success!");
} catch (IllegalArgumentException e){
if (showError) {
v = true;
e.printStackTrace();
}
else {
System.out.println("Error! Retrying... "+i+'/'+tries);
v = false;
}
wait(2000);
}
}
if(i-1==tries)
//System.out.println("No success :(");
throw new Exception("No success :(");
else
if(v==false)
System.out.println("SourceData line found and accepted !");
//-----SIGNAL PROCESSING-----
//nothing here because the rest isn't working
} catch(Exception e) { e.printStackTrace();}
}
}
最近,我一直在寻找一些代码来发现可用的格式,并且我在我的代码和edoloughlin的代码之间做了一些事情:
package audio;
import javax.sound.sampled.*;
public class App2 {
public static String getWord(String arr) {
int i = 0;
for(; i < arr.length(); i++) {
if (arr.charAt(i) == ' ')
break;
}
return arr.substring(0,i);
}
public static void wait(int ms) {
try{
Thread.sleep(ms);
}
catch(InterruptedException ex){
Thread.currentThread().interrupt();
}
}
public static AudioFormat getAudioFormat(){
float sampleRate = 44100;
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
int channels = 1;
//1,2
boolean signed = true;
//true,false
boolean bigEndian = false;
//true,false
//return new AudioFormat(Encoding.PCM_SIGNED, sampleRate, 16, 1, 2, sampleRate, false);
return new AudioFormat(sampleRate,
sampleSizeInBits,
channels,
signed,
bigEndian);
}
public static void main(String [] args) {
try {
String expectedMixer = "Speakers";
Mixer.Info[] mixers = AudioSystem.getMixerInfo();
Mixer.Info m = null;
for(int i = 0; i < mixers.length; i++) {
if(getWord(mixers[i].getName()).compareTo(expectedMixer) == 0){
m = mixers[i];
}
}
int sampleRates[] = { 8000, 11025, 16000, 22050, 44100 };
int channels[] = { 1, 2 };
int bytesPerSample[] = { 1, 2 };
boolean signature[] = {true, false};
AudioFormat format;
DataLine.Info lineInfo;
//for (Mixer.Info mixerInfo : AudioSystem.getMixerInfo()) {
for (int a = 0; a < sampleRates.length; a++) {
for (int b = 0; b < channels.length; b++) {
for (int c = 0; c < bytesPerSample.length; c++) {
for(int d = 0; d < signature.length; d++) {
format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
(float)sampleRates[a], 8 * bytesPerSample[c], channels[b], bytesPerSample[c],
(float)sampleRates[a], signature[d]);
lineInfo = new DataLine.Info(/*dataLineClass*/TargetDataLine.class, format);
if (AudioSystem.isLineSupported(lineInfo)) {
/*
* TODO: To perform an exhaustive search on supported lines, we should open
* TODO: each Mixer and get the supported lines. Do this if this approach
* TODO: doesn't give decent results. For the moment, we just work with whatever
* TODO: the unopened mixers tell us.
*/
if (AudioSystem.getMixer(/*mixerInfo*/m).isLineSupported(lineInfo)) {
//formats.add(format);
System.out.println(format);
}
}
}
}
}
}
//}
}catch(Exception e) {e.printStackTrace();}
}
}
如上所示,我使用了许多格式组合,但在控制台中没有打印出任何内容。我想知道我的系统/java 应用程序是否支持这样的任务。如果没有,有没有办法实现目标?(读取实时输出)