我非常擅长创建多线程程序,(这是我第一次应用它),我将Runnable实现到我的类中,并在我的一个按钮Actionlistener中运行它(new Thread(new Project())。 (),但我导入的每个资源都被删除了。就像文件一样。它只写了NullPointerException,我甚至测试了一个字符串,但是它的结果返回null
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
chooser = new JFileChooser();
chooser.setAcceptAllFileFilterUsed(false);
chooser.setCurrentDirectory(new File("."));
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
FileNameExtensionFilter filter = new FileNameExtensionFilter ("wav files","wav");
chooser.setFileFilter(filter);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
Song = chooser.getSelectedFile();
}
else
{
JOptionPane.showMessageDialog(null, "you didnt choose any file !!");
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
(new Thread(new TestMT())).start();
}
public void run() {
// TODO Auto-generated method stub
try {
audioStream = AudioSystem.getAudioInputStream(Song);
audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceLine = (SourceDataLine) AudioSystem.getLine(info);
sourceLine.open(audioFormat);
} catch (UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
try {
nBytesRead = audioStream.read(abData, 0, abData.length);
} catch (IOException e) {
e.printStackTrace();
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
}
sourceLine.drain();
sourceLine.close();
}
答案 0 :(得分:0)
您正在设置类中的所有变量,但是在按钮单击时,您将创建此类的新实例。这个新实例对旧变量的内容一无所知。它们都被重新初始化了。
(new Thread(new TestMT())).start();
您应该创建一个实现runnable的新类。给那个类所有需要的引用(可能通过构造函数),然后启动那个。
这可能是你的玩家类
通过
在TestMT中调用它new Thread(new Player(song)).start();
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Player implements Runnable {
private File song;
private static int BUFFER_SIZE = 4096;
Player(File song) {
this.song = song;
}
@Override
public void run() {
try {
AudioInputStream audioStream = AudioSystem
.getAudioInputStream(song);
AudioFormat audioFormat = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
SourceDataLine sourceLine = (SourceDataLine) AudioSystem
.getLine(info);
sourceLine.open(audioFormat);
sourceLine.start();
int nBytesRead = 0;
byte[] abData = new byte[BUFFER_SIZE];
while (nBytesRead != -1) {
nBytesRead = audioStream.read(abData, 0, abData.length);
}
if (nBytesRead >= 0) {
@SuppressWarnings("unused")
int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
}
sourceLine.drain();
sourceLine.close();
} catch (UnsupportedAudioFileException | IOException e1) {
e1.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}