[英]Java Read and Play MIDI file using public static void midi(Score score) method
如何创建一个程序来读取和播放 MIDI 文件? 我需要在 Class Play 中找到一种播放文件的方法,以及一种将 midi 文件加载到 Class 中的分数的方法,读取并将分数变量传递给 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 . 这是我到目前为止所拥有的。
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
这是我的文件名,但我不知道如何将它包含在我的代码中。
你可以尝试这样的事情:
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
注释和取消注释部分代码以获得更好的理解。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.