私は現在、基本的な椅子取り椅子アプリケーションを作成しており、ボタンを使用して音楽のオン/オフを切り替える際に問題が発生しました (私の美しいスイング インターフェイスで)。
netbeans でインターフェースを作成し、必要なアクション リスナーを追加しました。以下は関連するコードです。
*private void runActionPerformed(java.awt.event.ActionEvent evt) {
if (run.getText().equals("Stop Music"))
stopMus();
else
startMus();
}
public void startMus()
{
Music music = new Music();
music.playAudio();
run.setText("Stop Music");
}
public void stopMus()
{
Music music = new Music();
music.stopAudio();
run.setText("Start Music");
}*
クラス「音楽」は次のとおりです。
import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
public class Music {
AudioFormat audioFormat;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
boolean stopPlayback = false;
playThread p;
public Music()
{
p = new playThread();
}
public void playAudio()
{
try {
File soundFile = new File("music.mp3"); //file to play
audioInputStream = AudioSystem.getAudioInputStream(soundFile); // start a stream
audioFormat = audioInputStream.getFormat(); //finds format of file
DataLine.Info dli = new DataLine.Info(SourceDataLine.class, audioFormat); //creates a buffer to play
sourceDataLine =(SourceDataLine)AudioSystem.getLine(dli); //as above
} catch (Exception e)
{
System.out.println(e.getMessage());
}
p.start();
}
public void stopAudio()
{
p.stopT();
}
private class playThread extends Thread
{
byte[] tempBuffer = new byte[10000];
@Override public void run(){
try{
sourceDataLine.open(audioFormat);
sourceDataLine.start();
int cnt;
//loop while buffer isn't empty
while((cnt = audioInputStream.read(tempBuffer,0,tempBuffer.length)) != -1 && stopPlayback == false){
if(cnt > 0)
sourceDataLine.write(tempBuffer, 0, cnt);
}
sourceDataLine.drain();
sourceDataLine.close();
stopPlayback = false;
}catch (Exception e) {
System.out.println(e.getMessage());
System.exit(0);
}
}
public void stopT()
{
this.interrupt();
}
}
}
私が得ているエラーはかなり奇妙なものです! (そして長い)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type: coursework.Music
at coursework.JFrameThing.startMus(JFrameThing.java:57)
at coursework.JFrameThing.runActionPerformed(JFrameThing.java:51)
at coursework.JFrameThing.access$000(JFrameThing.java:3)
at coursework.JFrameThing$1.actionPerformed(JFrameThing.java:23)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2045)
at java.awt.Component.dispatchEventImpl(Component.java:4649)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
at java.awt.Container.dispatchEventImpl(Container.java:2089)
at java.awt.Window.dispatchEventImpl(Window.java:2587)
at java.awt.Component.dispatchEvent(Component.java:4475)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:675)
at java.awt.EventQueue.access$300(EventQueue.java:96)
at java.awt.EventQueue$2.run(EventQueue.java:634)
at java.awt.EventQueue$2.run(EventQueue.java:632)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
at java.awt.EventQueue$3.run(EventQueue.java:648)
at java.awt.EventQueue$3.run(EventQueue.java:646)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:645)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
いくつかのグーグルの後、それは古風なものであると言われています。また、Music オブジェクト (Music music = new Music();) のインスタンス化も指していますが、なぜそれが問題を引き起こすのか理解できません! 私のスレッド化は可能な限り効率的ではないことを知っていますが、確かにそれはこの問題を引き起こさないでしょうか?