gpt4 book ai didi

java - 为什么小程序关闭服务器?

转载 作者:行者123 更新时间:2023-11-28 23:57:02 25 4
gpt4 key购买 nike

我正在为我的项目实现一个使用 Java applet 的录音系统,但我在关闭 applet 时遇到一个问题,applet 关闭了服务器。有什么办法可以解决这个问题吗?我想在完成录制部分后继续我的项目,但在关闭小程序时录制后它也会停止服务器,所以我需要再次重新启动服务器。

有什么帮助或建议吗?

代码:-

 public class Main extends JPanel implements ActionListener {


public Main() {
setLayout(new BorderLayout());
EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);
SoftBevelBorder sbb = new SoftBevelBorder(SoftBevelBorder.LOWERED);
setBorder(new EmptyBorder(5, 5, 5, 5));

JPanel p1 = new JPanel();
// p1.setLayout(new BoxLayout(p1, BoxLayout.X_AXIS));
JPanel p2 = new JPanel();
p2.setBorder(sbb);
p2.setLayout(new BoxLayout(p2, BoxLayout.X_AXIS));
JPanel buttonsPanel = new JPanel();
buttonsPanel.setBorder(new EmptyBorder(10, 0, 5, 0));
radioGroup1 = new CheckboxGroup();
radio1 = new Checkbox("Record : ", radioGroup1,true);
p2.add(radio1);
playB = addButton("Play", buttonsPanel, false);
captB = addButton("Record", buttonsPanel, true);
closeA = addButton("Close", buttonsPanel, true);
p2.add(buttonsPanel);
p1.add(p2);
add(p1);

}

public void open() {
}

public void close() {
if (playback.thread != null) {
playB.doClick(0);
}
if (capture.thread != null) {
captB.doClick(0);
}
}

private JButton addButton(String name, JPanel p, boolean state) {
JButton b = new JButton(name);
b.addActionListener(this);
b.setEnabled(state);
p.add(b);
return b;
}

public void actionPerformed(ActionEvent e) {

Object obj = e.getSource();

if (obj.equals(playB)) {
if (playB.getText().startsWith("Play")) {
playback.start();
captB.setEnabled(false);
playB.setText("Stop");
} else {
playback.stop();
captB.setEnabled(true);
playB.setText("Play");
}
} else if (obj.equals(captB)) {
if (captB.getText().startsWith("Record")) {
capture.start();
playB.setEnabled(false);
captB.setText("Stop");
} else {
capture.stop();
playB.setEnabled(true);
}

}
else if(obj.equals(closeA)) {
System.exit(0);
}

}

public class Playback implements Runnable {
SourceDataLine line;
Thread thread;
public void start() {
errStr = null;
thread = new Thread(this);
thread.setName("Playback");
thread.start();
}
public void stop() {
thread = null;
}
private void shutDown(String message) {
if ((errStr = message) != null) {
System.err.println(errStr);
}
if (thread != null) {
thread = null;
captB.setEnabled(true);
playB.setText("Play");
}
}

public void run() {


AudioFormat format = getAudioFormat();
try {
audioInputStream = AudioSystem.getAudioInputStream(wavFile);
} catch (UnsupportedAudioFileException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format,
audioInputStream);

if (playbackInputStream == null) {
shutDown("Unable to convert stream of format " + audioInputStream + " to format " + format);
return;
}

// get and open the source data line for playback.

try {
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, bufSize);



} catch (LineUnavailableException ex) {
shutDown("Unable to open the line: " + ex);
return;
}

// play back the captured audio data

int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numBytesRead = 0;

// start the source data line
line.start();

while (thread != null) {
try {
if ((numBytesRead = playbackInputStream.read(data)) == -1) {
break;
}
int numBytesRemaining = numBytesRead;
while (numBytesRemaining > 0) {
numBytesRemaining -= line.write(data, 0, numBytesRemaining);
}
} catch (Exception e) {
shutDown("Error during playback: " + e);
break;
}
}
// we reached the end of the stream.
// let the data play out, then
// stop and close the line.
if (thread != null) {
line.drain();
}
line.stop();
line.close();
line = null;
shutDown(null);
}

} // End class Playback

/**
* Reads data from the input channel and writes to the output stream
*/
class Capture implements Runnable {

TargetDataLine line;

Thread thread;

public void start() {
errStr = null;
thread = new Thread(this);
thread.setName("Capture");
thread.start();
}


public void stop() {
thread = null;
line.close();
//thread.stop();
}

private void shutDown(String message) {
if ((errStr = message) != null && thread != null) {
thread = null;
playB.setEnabled(true);
captB.setText("Record");
System.err.println(errStr);
}
}

public void run() {

duration = 0;
audioInputStream = null;

Playback pb = new Playback();
AudioFormat format = pb.getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);


// get and open the target data line for capture.

try {

line = (TargetDataLine) AudioSystem.getLine(info);
// line.open(format, line.getBufferSize());
line.open(format);
line.start();
// saving audio file

AudioInputStream ais = new AudioInputStream(line);

// start recording
AudioSystem.write(ais, fileType, wavFile);

} catch (LineUnavailableException ex) {
shutDown("Unable to open the line: " + ex);
return;
} catch (SecurityException ex) {
shutDown(ex.toString());
//JavaSound.showInfoDialog();
return;
} catch (Exception ex) {
shutDown(ex.toString());
return;
}


// we reached the end of the stream.
// stop and close the line.
line.stop();
line.close();
line = null;

// stop and close the output stream
try {
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}

// load bytes into the audio input stream for playback

byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
audioInputStream = new AudioInputStream(bais, format, audioBytes.length / frameSizeInBytes);

long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format
.getFrameRate());
duration = milliseconds / 1000.0;

try {
audioInputStream.reset();
} catch (Exception ex) {
ex.printStackTrace();
return;
}

}

} // End class Capture

最佳答案

这段代码看起来不太好

else if(obj.equals(closeA)) {
System.exit(0);
}

这将导致 JVM 关闭。我还以为你只是想让小程序处于停止状态。

关于java - 为什么小程序关闭服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22343115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com