Playing sound with MIDI - Java

Asked

Viewed 55 times

0

I wrote a code to play a certain sound, but it’s not playing.

package reprodutormusical;

public class TestDrive {

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

    }

}

class Minimusicapp:

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

Has 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 reproducormusical.MiniMusicApp.play(Minimusicapp.java:26) at reproducormusical.TestDrive.main(Testdrive.java:7)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.