You are on page 1of 2

Chy file midi trong ng dng Java.

Cp nht: 6/9/2008 vi no comments Xp trong: Java, Java Core Bi vit ny ti xin trnh by cch thc lm sao chy c file nhc .midi trong ng dng Java. Chng ta s s dng n gi javax.sound thc hin mt chng trnh n gin ny. M ngun chng trnh bn c th xem y, v bn c th chnh sa... Bi vit ny ti xin trnh by cch thc lm sao chy c file nhc .midi trong ng dng Java. Chng ta s s dng n gi javax.sound thc hin mt chng trnh n gin ny. M ngun chng trnh bn c th xem y, v bn c th chnh sa cht t t c mun v trong chng trnh ny bn s chy file .midi bng cch cung cp ng dn i s cho chng trnh. view plaincopy to clipboardprint? 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. import javax.sound.midi.*; import java.io.*; /** Plays a midi file provided on command line */ public class MidiPlayer { public static void main(String args[]) { // Argument check if(args.length == 0) { helpAndExit(); } String file = args[0]; if(!file.endsWith(".mid")) { helpAndExit(); } File midiFile = new File(file); if(!midiFile.exists() || midiFile.isDirectory() || !midiFile.canRead()) { helpAndExit(); } // Play once try { Sequencer sequencer = MidiSystem.getSequencer(); sequencer.setSequence(MidiSystem.getSequence(midiFile)); sequencer.open(); sequencer.start(); while(true) { if(sequencer.isRunning()) { try { Thread.sleep(1000); // Check every second } catch(InterruptedException ignore) { break; } } else { break; } } // Close the MidiDevice & free resources sequencer.stop(); sequencer.close(); } catch(MidiUnavailableException mue) { System.out.println("Midi device unavailable!"); } catch(InvalidMidiDataException imde) { System.out.println("Invalid Midi data!"); } catch(IOException ioe) { System.out.println("I/O Error!");
value:X%E1%BA

44. 45. 46. 47. 48. 49. 50. 51.

} } /** Provides help message and exits the program */ private static void helpAndExit() { System.out.println("Usage: java MidiPlayer midifile.mid"); System.exit(1); } }

import javax.sound.midi.*; import java.io.*; /** Plays a midi file provided on c public class MidiPlayer { public static void main(String

You might also like