我正在使用Cloud9 IDE编写Java程序。我想用MP3音频文件实现某种工作。我下载并设置了MP3SPI。我编写了简单的测试程序,它使用了这个API。
import javax.sound.sampled.*;
import java.io.*;
public class testmp {
public static void main(String[] args) {
testPlay("../../tmp_mp3/Redfoo.mp3");
}
public static void testPlay(String filename) {
try {
File file = new File(filename);
AudioInputStream in = AudioSystem.getAudioInputStream(file);
AudioInputStream din = null;
AudioFormat baseFormat = in .getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
baseFormat.getSampleRate(),
16,
baseFormat.getChannels(),
baseFormat.getChannels() * 2,
baseFormat.getSampleRate(),
false);
din = AudioSystem.getAudioInputStream(decodedFormat, in ); in .close();
} catch (Exception e) {
//Handle exception.
}
}
}
当我使用java testmp
运行时,我会收到
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
我尝试打开无头模式:java -Djava.awt.headless=true testmp
。接收:
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
此应用程序不需要使用显示器和/或键盘,所以我不明白,为什么它使用X11。 我找到了this问题的解决方案,但是下载的文件存档了。
如何让它在终端中运行?我不需要播放MP3,我想修改音频数据(帧)并将其写入其他MP3文件。
完整错误堆栈跟踪:
java.awt.HeadlessException
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:207)
at java.awt.Window.<init>(Window.java:535)
at java.awt.Frame.<init>(Frame.java:420)
at java.awt.Frame.<init>(Frame.java:385)
at com.trend.iwss.jscan.runtime.BaseDialog.getActiveFrame(BaseDialog.java:75)
at com.trend.iwss.jscan.runtime.AllowDialog.make(AllowDialog.java:32)
at com.trend.iwss.jscan.runtime.PolicyRuntime.showAllowDialog(PolicyRuntime.java:325)
at com.trend.iwss.jscan.runtime.PolicyRuntime.stopActionInner(PolicyRuntime.java:240)
at com.trend.iwss.jscan.runtime.PolicyRuntime.stopAction(PolicyRuntime.java:172)
at com.trend.iwss.jscan.runtime.FileIOPolicyRuntime.doStopAction(FileIOPolicyRuntime.java:281)
at com.trend.iwss.jscan.runtime.FileIOPolicyRuntime._preFilter(FileIOPolicyRuntime.java:260)
at com.trend.iwss.jscan.runtime.PolicyRuntime.preFilter(PolicyRuntime.java:132)
at com.trend.iwss.jscan.runtime.FileIOPolicyRuntime.preFilter(FileIOPolicyRuntime.java:171)
at com.sun.media.codec.audio.mp3.JS_MP3FileReader.getAudioInputStream(JS_MP3FileReader.java:113)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1179)
at testmp.testPlay(testmp.java:16)
at testmp.main(testmp.java:9)