简体   繁体   中英

Java Read and Play MIDI file using public static void midi(Score score) method

How can I create a program to read and play a midi file? I need to find a method to play the file in Class Play and a method that loads the midi file into a score in Class Read and pass the score variable to View.sketch(). I was using this website to find the methods under class Read and class Play http://www.explodingart.com/jmusic/jmDocumentation/index.html . Here is what I have so far.

import jm.JMC;
import jm.music.data.*;
import jm.util.*;

public class Main {
     public static void main ( String [] args ){
        public static void midi(Score score){
           Score score = new Score("my music score ");// Name your score
           View.sketch(score);
        }

    }
}
//1272439.1.mid 

this is my file name but I don't know how to include it in my code.

You can try something like this:

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import jm.music.data.Score;
import jm.util.Play;
import jm.util.Read;
import jm.util.View;

public class Test {
    public static void main(String[] args) throws Exception {
        File f = new File("e:\\tb.mid");
        
        if(!f.exists()) {
            System.out.println("File " + f.getAbsolutePath() + " doesn't exist.");
            System.exit(-1);
        }
        Score score =  Read.midiOrJmWithNoMessaging(f);
        score.setTitle("my music score ");// Name your score
        
        View.sketch(score);
        
        //play whole file
        //Play.midi(score,false);
        
        //to play only for 5 seconds
        ExecutorService es = Executors.newSingleThreadExecutor();
        es.submit(()-> Play.midi(score, false));
        //stop playing after 5 seconds
        Thread.sleep(5000);  
        System.out.println("stopping");
        Play.stopMidi(); //stop playing midi
        Play.closeAll(); //close all resources
        
        //uncomment this if you want to exit the program after playing
        //System.exit(0); 
    }
}

Output:

Convert SMF to JM
jMusic Play: Playing score my music score  using JavaSound General MIDI soundbank.
jMusic Play: Waiting for the end of my music score .
stopping
jMusic Play: Stopping JavaSound MIDI playback of 0
jMusic MidiSynth: Stopped JavaSound MIDI playback

Comment and uncomment part of the code to get better understanding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM