6

I'm currently writing a basic musical chairs application and have run into some difficulty while turning on/off music using a button (in my beautiful swing interface).

I created the interface in netbeans, and have added the necessary action listeners. Below is the relevant code:

*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");
}*

The class "Music" is as follows:

    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();
            }
        }
 }

The error I'm getting is a pretty strange one! (and long)

 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)

After some googling, it is allegedly an archaic one; it also points to my instantiation of the Music object (Music music = new Music();) but I can't understand why that would cause an issue! I know that my threading isn't as efficient as it could be, but surely that wouldn't cause this problem?

Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28
Patrick McClurg
  • 123
  • 1
  • 10

1 Answers1

0

Okay, I think it's just an error caused by netbeans when you compile your project with errors still in it... sorry <3

Patrick McClurg
  • 123
  • 1
  • 10