Playing sound with MIDI - Java

0

I wrote a code to play a particular sound, but it is not playing.

package reprodutormusical;

public class TestDrive {

    public static void main(String[] args) {
        MiniMusicApp mini = new MiniMusicApp();
        mini.play();

    }

}

MiniMusicApp class:

package reprodutormusical;
import javax.sound.midi.*;

public class MiniMusicApp {

    public void play() {
        try {
        // Cria uma anologia a um DVD Player
        Sequencer player = MidiSystem.getSequencer();
        // Cria um analogia ao CD que será tocado
        Sequence seq = new Sequence(Sequence.PPQ, 4);
        Track track = seq.createTrack();

        ShortMessage a = new ShortMessage();
        a.setMessage(144, 1, 44, 100);
        MidiEvent noteOn = new MidiEvent(a, 1);
        track.add(noteOn);

        ShortMessage b = new ShortMessage();
        a.setMessage(128, 1, 44, 100);
        MidiEvent noteOff = new MidiEvent(b, 16);
        track.add(noteOff);

        player.setSequence(seq);

        player.start();
        // Inseri uma pausa para dar ao som de reproduzir
        Thread.sleep(100 * 2);
        player.close();
        System.exit(0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Show this exception:

  

Jun 09, 2018 12:37:03 PM java.util.prefs.WindowsPreferences   WARNING: Could not open / create prefs root node Software \ JavaSoft \ Prefs   at root 0x80000002. Windows RegCreateKeyEx (...) returned error code 5.   java.lang.IllegalStateException: sequencer not open at   com.sun.media.sound.RealTimeSequencer.start (Unknown Source) at   reprodutormusical.MiniMusicApp.play (MiniMusicApp.java:26) at   reprodutormusical.TestDrive.main (TestDrive.java:7)

    
asked by anonymous 09.06.2018 / 17:41

0 answers