MidiSystem.getReceiver () freezes JFrame

I am making a program that includes playing MIDI sounds, and only today I ran into a problem in which a call

MidiSystem.getReceiver()

or opening MidiDevice, completely prevents me from displaying a frame on the screen. And then, if I try to finish everything before everything is frozen, Eclipse tells me that "completion failed."

Here is a sample code to show you what I mean:

public static void main(String args[]) {

    Receiver receiver;

    try {
        receiver = MidiSystem.getReceiver();
    } catch (MidiUnavailableException e) {
        e.printStackTrace();
    }

    JFrame frame = new JFrame("here a frame");
    Dimension d = new Dimension(500,500);
    frame.setSize(d);
    frame.setPreferredSize(d);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

Part getReceiver()and part JFrameeach work normally independently; it's just when I have both parts that stop working.

(Btw I did not have this problem when running similar code a couple of weeks ago ...?)

Any help would be greatly appreciated. Thank!

+1
1

Swing, . , Swing EDT ( E vent D ispatch T hread). Oracle: Concurrency Swing

,

import java.awt.*;
import javax.sound.midi.*;
import javax.swing.*;

public class MidiFoo {
   public static void main(String args[]) {

      new Thread(new Runnable() {
         public void run() {
            try {
               Receiver receiver = MidiSystem.getReceiver();
            } catch (MidiUnavailableException e) {
               e.printStackTrace();
            }
         }
      }).start();

      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            JFrame frame = new JFrame("here a frame");
            Dimension d = new Dimension(500, 500);
            frame.setSize(d);
            frame.setPreferredSize(d);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         }
      });
   }
}
+1

All Articles