Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

macos - Java Midi in Mac OSX Broken?

I'm trying to play midi within a browser, and have been using a Java Applet that works just fine on PCs. Its extremely unreliable on OSX, so I wrote a simple test case that exhibits the same problem:

import javax.sound.midi.*;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class MidiPlayer {

  public static void main(String[] args) {
      try {
          Sequencer sequencer = MidiSystem.getSequencer();
          if (sequencer == null)
              throw new MidiUnavailableException();
          sequencer.open();
          FileInputStream is = new FileInputStream("sample.mid");
          Sequence mySeq = MidiSystem.getSequence(is);
          sequencer.setSequence(mySeq);
          sequencer.start();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
}

It sounds like the occasional message is getting dropped.. Like a noteoff won't fire, and a random note will hang on forever. Is this a known problem in OSX? Seems like Java just isn't getting enough love from Apple these days.

If anyone has a better solution to playing Midi in a browser, I'm all ears!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This appears to be a two part problem. I too could not send midi sysex's using a mid-2011 OSX 10.7.5 equipped iMac. I did find a workaround - first, I had to use the mmj jar and jnilib's and secondly I had to tell my code to use timestamps of -1 and NOT to use system.currentTimeMillis(). In my case I'm sending realtime sysex messages, thus a timestamp of -1 works for me. I don't know what timestamp to use if you're dealing with midi note on/off's etc. Perhaps the timestamp is milliseconds into the future? I don't know. But I do know that I had to use both mmj and take better control of my timestamps. After that, things work as expected.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...