6

我目前正在编写一个基本的音乐椅应用程序,并且在使用按钮打开/关闭音乐时遇到了一些困难(在我漂亮的摇摆界面中)。

我在 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();),但我不明白为什么这会导致问题!我知道我的线程没有尽可能高效,但这肯定不会导致这个问题吗?

4

1 回答 1

0

好的,我认为这只是当您编译项目时netbeans引起的错误仍然存​​在错误......对不起<3

于 2013-03-11T14:05:20.987 回答