我正在使用 AnotherTerm 探索 Java,使用 AnLinux Debian rootfs 和一个 脚本来安装 IceWM,以便在 proot 中使用 VNC 服务器。我在根和 IceWM 中播放音频时遇到困难。任何指导将不胜感激。
我尝试过...
import javax.sound.sampled.*;
public class AudioTest {
public static void main(String[] args) {
try {
// Set the parameters for the audio format
AudioFormat audioFormat = new AudioFormat(44100, 16, 1, true, false);
// Get a SourceDataLine that matches the specified audio format
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
// Open the SourceDataLine and start it
line.open(audioFormat);
line.start();
// Generate a simple sine wave and write it to the audio output
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
for (int i = 0; i < 1000; i++) {
double angle = i / (audioFormat.getSampleRate() / 440.0) * 2.0 * Math.PI;
short sample = (short) (Short.MAX_VALUE * Math.sin(angle));
buffer[0] = (byte) (sample & 0xFF);
buffer[1] = (byte) ((sample >> 8) & 0xFF);
line.write(buffer, 0, 2);
}
// Close the line when finished
line.drain();
line.close();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}
Exception in thread "main" java.lang.IllegalArgumentException: No line matching interface SourceDataLine supporting format PCM_SIGNED 44100.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian is supported.
at java.desktop/javax.sound.sampled.AudioSystem.getLine(AudioSystem.java:425)
at AudioTest.main(AudioTest.java:12)
还有...
$ paplay $AUDIO_FILE_PATH
Failed to open audio file.
$ java --version
openjdk 11.0.21 2023-10-17
OpenJDK Runtime Environment (build 11.0.21+9-post-Debian-1deb11u1)
OpenJDK Server VM (build 11.0.21+9-post-Debian-1deb11u1, mixed mode)
$ pulseaudio --version
pulseaudio 14.2
安卓10 armeabi-v7a 未root
发现了解决方案
pulseaudio
:PulseAudio 服务器能够使用各种协议通过 TCP/UDP 进行流式传输。
但是,我对在 Java 中播放音频文件时反复遇到的异常感到困惑。