设置音序器文件

时间:2019-01-27 22:18:27

标签: java javafx midi

我正在创建一个Java项目,该项目在计算机上搜索所有midi文件并将其显示在列表中。当用户选择一个midis时,它将在网站“ www.virtualpiano.net”中播放。问题是,用户从列表中选择了一个midis之后,GUI不再响应,并在顶部显示(不响应)。其余功能仍然可以正常运行,例如可以播放。 以下代码来自Main类,使用了其他类,但它们不应与错误无关。

import java.awt.Desktop;
import java.awt.FileDialog;
import java.awt.Frame;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

import javax.sound.midi.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import org.apache.commons.io.FileUtils;




public class PianoMidiMain extends Application implements EventHandler<ActionEvent> {
    Button button;
    Button searchButton;
    List<File> allMidis = null;
    ListView<File> midiSongs;
    GridPane layout;
    Sequencer sequencer;
    File saveMidiPaths;
    PrintWriter printWriter;
    FileReader reader;
    Sequence sequence;




    public static PianoMidiMain midimain;

    public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException, InterruptedException, URISyntaxException {

        midimain = new PianoMidiMain();

        Application.launch(args);
//      MidiInspector inspector = new MidiInspector();
//      inspector.Inspect();
//      




//      
    }

    public List<File> midiFinder() throws IOException {
        //Find all midi files in the users folder

        File dir = new File(System.getProperty("user.home")+"\\desktop\\Songs");
        String[] extensions = new String[] { "midi", "mid" };
        System.out.println("Getting all .midi and .mid files in " + dir.getCanonicalPath()
                + " including those in subdirectories");
        System.out.println("Started List Files");
        List<File> midiFiles = (List<File>) FileUtils.listFiles(dir, extensions, true);
        System.out.println("Finished List Files");
//      for (File file : midiFiles) {
            //System.out.println("file: " + file.getCanonicalPath());
//      }

        return midiFiles;
    }

    public ListView<File> createListView(List<File> theMidis) {
        // Create the Lists for the ListViews

        ObservableList<File> midiList = null;

        if(theMidis != null) {
             midiList = FXCollections.observableList(theMidis);
        }
        else
        {
            System.out.println("DERP no midifiles list");
        }

         // Create the ListView for the seasons

        ListView<File> midis = new ListView<>(midiList);

           // Set the Orientation of the ListView

        midis.setOrientation(Orientation.VERTICAL);

                // Set the Size of the ListView

        midis.setMinSize(layout.getLayoutX(), layout.getLayoutY() / 2);

        midis.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<File>(){

            @Override
            public void changed(ObservableValue<? extends File> arg0, File oldFile, File newFile) {
                System.out.println("New file is " + newFile.getName());
                try {
                    StartSelectedMidi(newFile.getAbsolutePath());
                } catch (InvalidMidiDataException | IOException | MidiUnavailableException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }});


        return midis;

    }

    public void StartSelectedMidi(String FilePath) throws InvalidMidiDataException, IOException, MidiUnavailableException
    {
//      Desktop desktop = Desktop.getDesktop();
//      desktop.browse(new URI("https://virtualpiano.net/"));
        LogManager.getLogManager().reset();

        // Get the logger for "org.jnativehook" and set the level to off.
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.OFF);

         sequence = null;

        sequence = MidiSystem.getSequence(new File(FilePath));

     // Thread.sleep((long) 1000);
        sequencer = MidiSystem.getSequencer();
        MidiControllerEventListener controller = new MidiControllerEventListener();
        songControl songcontrol = new songControl(sequencer);

        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(songcontrol);


        sequencer.open();

        Transmitter trans = sequencer.getTransmitter();
        trans.setReceiver(controller);

        sequencer.setSequence(sequence);
        //sequencer.start();

        while (true) {
            if(sequencer.getTickLength() == sequencer.getTickPosition()){
                sequencer.stop();
                sequencer.close();
            }
        }
    }


    @Override
    public void start(Stage primaryStage) throws Exception {

        //List<File> allMidis = midimain.midiFinder();
//      for (File file : allMidis) {
//          System.out.println("Still have paths?: " + file.getCanonicalPath());
//      }


        if(new File("SavedMidis.txt").isFile())
        {
            System.out.println("Did i find a file?");
            //saveMidiPaths = new File("SavedMidis.txt");
//          System.out.println(saveMidiPaths.getAbsolutePath());

//          if(saveMidiPaths.exists()==false) {
//              
//              //saveMidiPaths.createNewFile();
//          }

            printWriter = new PrintWriter("SavedMidis.txt");

//          if(saveMidiPaths.exists()) {
                reader = new FileReader("SavedMidis.txt");
                //System.out.println("woowee savedMidiPaths exists");
                int i; 
                while ((i=reader.read()) != -1) 
                  System.out.print((char) i);
//          }
        }



//      System.out.print(saveMidiPaths.getAbsolutePath());
//      
        primaryStage.setTitle("Version2");
        button = new Button("Exit");
        button.setLayoutX(5.0);
        searchButton = new Button("Search Midis");

        button.setOnAction(this);
        searchButton.setOnAction(this);

        layout = new GridPane();
        layout.setMinSize(700, 1000);
//      layout.setAlignment(Pos.CENTER);

        layout.add(searchButton, 0, 0);
        layout.add(button,1,0);





        Scene scene = new Scene(layout);
        primaryStage.setScene(scene);
        primaryStage.setMinWidth(800);
        primaryStage.setMinHeight(800);
        primaryStage.show();
    }

    @Override
    public void handle(ActionEvent event){
        if(event.getSource()==button) {
            if(sequencer== null) {

            }
            System.exit(0);
        }

        if(event.getSource()==searchButton) {

                try {
                    searchMidis();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    }

    public void searchMidis() throws IOException {


        allMidis = midimain.midiFinder();

        for (File file : allMidis) {
//          System.out.println("Still have paths?: " + file.getCanonicalPath());
//          System.out.println("Still have paths?: " + file.getName());
            printWriter.println(file.getAbsolutePath() + System.lineSeparator());

        }
        printWriter.close();


        midiSongs = createListView(allMidis);


        layout.add(midiSongs, 0, 1);

        }

    public void ReadSavedMidisFile()
    {

    }

}

0 个答案:

没有答案