簡體   English   中英

Java 使用公共 static void midi(Score score) 方法讀取和播放 MIDI 文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM