how do you play MID files using Java?

I tried this…Help please!?

code:
public void playMidiFrom(String path) {
try {
Sequence midi = MidiSystem.getSequence(this.getClass().getClassLoader().getResource(path));
Sequencer player = MidiSystem.getSequencer();
player.open();
player.setSequence(midi);
player.start();
} catch (IOException e) {
System.out.println(“else”);
e.printStackTrace();
} catch (InvalidMidiDataException e) {
System.out.println(“Invalid MIDI data”);
e.printStackTrace();
} catch (MidiUnavailableException e) {
System.out.println(“No midi”);
e.printStackTrace();
}
}

i get this error:
java.lang.IllegalArgumentException: Requested device not installed: null
at javax.sound.midi.MidiSystem.getGenericDevice(MidiSystem.java:1073)
at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:277)
at CroAsteroids1.lib.sounds.Sounds.playMidiFrom(Sounds.java:89)
at CroAsteroids1.lib.sounds.Sounds.(Sounds.java:83)
at CroAsteroids1.lib.Asteroids.init(Asteroids.java:283)
at CroAsteroids1.lib.Asteroids.run(Asteroids.java:116)
at CroAsteroids1.lib.Asteroids.main(Asteroids.java:110)

I can play mid’s in Winamp. And i am not trying to play it in winamp at the same time while running the code. Using windows xp.

You could try the code over there:
http://www.java-gaming.org/forums/index.php?topic=406.0

I am having problems with the following line of code:

o = MidiSystem.getSequencer();

This is the error i get:

Exception in thread "main" java.lang.IllegalArgumentException: Requested device not installed: null
	at javax.sound.midi.MidiSystem.getGenericDevice(MidiSystem.java:1073)
	at javax.sound.midi.MidiSystem.getSequencer(MidiSystem.java:277)
	at CroAsteroids1.lib.SimpleMidiPlayer.main(SimpleMidiPlayer.java:176)

Any ideas?

getSequencer(true) ?

(starting from 1.5 : this will connect the sequencer to the default internal synthetizer, if false, sound will be output by the os, not the java runtime).

Lilian

There was no need to push both threads.

Do you have a soundbank installed? I still dont know that.

I have one installed and this program works just fine…

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

public class MidiTest{

	private Sequencer sequencer = null;
	private Synthesizer synthesizer;
	public MidiTest(){
		try{
			synthesizer = MidiSystem.getSynthesizer();
			synthesizer.open();
			sequencer = MidiSystem.getSequencer();
			sequencer.open();
			sequencer.setSequence(new BufferedInputStream(getClass().getResourceAsStream("midi.mid")));
			sequencer.start();

			JFrame f=new JFrame("MidiTest");
			JLabel label=new JLabel("close to stop playback");
			label.setHorizontalAlignment(SwingConstants.CENTER);
			f.setSize(300,100);
			f.getContentPane().add(label);
			f.setVisible(true);
			f.addWindowListener(
				new WindowAdapter(){
					public void windowClosing(WindowEvent e){
						sequencer.stop();
						synthesizer.close();
						System.exit(0);
					}
				}
			);
		}catch(Exception e){
			e.printStackTrace();
			System.exit(1);
		}
	}
	public static void main(String[]args){
		new MidiTest();
	}
}

@c_lilian

getSequencer(true)? What should that change?

getSequencer() “This method is equivalent to calling getSequencer(true).” (javadoc)

ahh well, so it was getSequencer(false) :wink:

As i’ve subscribed to the javasound mailing list, this is something that shows up somtimes, and as of 1.5, you can plug the sequencer into the java software synthetiser or to the soundcard. I was thinking the problem could be related to some incorrect settings preventing the sequencer to be properly connected to the soundcard…

Lilian

I also noticed a problem which might make my problem more clear to somebody…

MidiDevice.Info[] info = MidiSystem.getMidiDeviceInfo();
         System.out.println("Info=" + info.length);
         for (int i = 0; i < info.length; i++) {
              System.out.println(info[i]);
         }

returns “0”. Like i don’t have a midi device on the system?

My friend with the same exact code gets:
Info=4
Java Sound Synthesizer
Java Sound Sequencer
Microsoft MIDI Mapper
Microsoft GS Wavetable SW Synth

I don’t get it. I’ve been googling for a few days now and no luck. Here and there i find a comment saying it is not possible except by using Timidi or something like that. I looked that up to and i really don’t understand where the problem is. My buddy uses the same code and it works for him. I can play the midi’s using winamp.

the new error i get is:
java.lang.IllegalArgumentException: Requested device not installed: null
at javax.sound.midi.MidiSystem.getGenericDevice(MidiSystem.java:1073)
at javax.sound.midi.MidiSystem.[b]getSynthesizer/b

I can’t use the getSequencer(boolean) cause i don’t have Java 1.5. At work we are using 1.4.

I can play wav’s using java…

I suggest posting this question on the javasound mailing list as it should give you an answer in less than a day…

look ere http://archives.java.sun.com/archives/javasound-interest.html

there’s a bunch of folks always ready to help people there, including the previous lead developer of the javax.sound from sun.

Lilian

Ok. I did it. Now i suppose wait for a response. I’ll get back with it ASAP.

this is what i got:

try
info[i].getName()
info[i].getDescription()


You should get Java 1.5, as the midi sequencer gives good timing. Also
read :

http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#js

Albert


that’s alright:

  • earlier versions of the JDK did not support external MIDI ports or
    hardware synthesizers
  • if the internal Java Sound synthesizer cannot be initialized, you
    will
    not have a single MIDI device accessible from Java

The internal Java synth needs:

  • a soundcard with CD quality capability
  • an installed Java soundbank (check java.sun.com/sound or this mailing
    list’s archive how to install a soundbank)

Florian