• We just launched and are currently in beta. Join us as we build and grow the community.

Load and Play a MIDI Sound File in Java

Ngewe

SMM Automation Expert
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
132
Likes
160
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial will teach you how to create a program that will load and play a MIDI sound file in java. We all know that MIDI or Musical Instrument Digital Interface) is a sound designed for recording and playing back music on digital synthesizers that is supported by many makes of personal computer sound cards. It has .mid extension file.

So, now let's start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of playMIDI.java.

2. Import the following package library:
  1. import

    java.io.*

    ;

    //used to access the File class
  2. import

    javax.sound.midi.*

    ;

    //used to access MidiSystem, Sequence, and Sequencer class

3. Now, you have to put your MIDI file on the same folder with the your program. Then access the MIDI file with this code:
  1. // From file
  2. Sequence

    sequence =

    MidiSystem

    .getSequence

    (

    new

    File

    (

    "midifile.mid"

    )

    )

    ;

midifile.mid is the filename of the MIDI filie and you can change this to your MIDI file name.

4. Create a sequencer for the sequence of the MIDI file.
  1. Sequencer

    sequencer =

    MidiSystem

    .getSequencer

    (

    )

    ;
  2. sequencer.open

    (

    )

    ;
  3. sequencer.setSequence

    (

    sequence)

    ;

5. To start playing your MIDI file, have this code below:
  1. // Start playing
  2. sequencer.start

    (

    )

    ;

Output:
When pressing the run button in your Java software, the MIDI filename will automatically play.


Here's the full code of this tutorial:

  1. import

    java.io.File

    ;

  2. import

    javax.sound.midi.MidiSystem

    ;
  3. import

    javax.sound.midi.Sequence

    ;
  4. import

    javax.sound.midi.Sequencer

    ;

  5. public

    class

    playMIDI {
  6. public

    static

    void

    main(

    String

    [

    ]

    argv)

    throws

    Exception

    {
  7. // From file
  8. Sequence

    sequence =

    MidiSystem

    .getSequence

    (

    new

    File

    (

    "midifile.mid"

    )

    )

    ;

  9. // Create a sequencer for the sequence
  10. Sequencer

    sequencer =

    MidiSystem

    .getSequencer

    (

    )

    ;
  11. sequencer.open

    (

    )

    ;
  12. sequencer.setSequence

    (

    sequence)

    ;

  13. // Start playing
  14. sequencer.start

    (

    )

    ;
  15. }
  16. }

For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

332,845

332,853

Top