Jmidilame
今天终于有时间写博客了,这个Jmidilame是我基于上次的JTimidity (https://www.cnblogs.com/debianroot/p/9613986) 升级而来的。这个版本抛弃了timidity后端(因为它貌似有近十年没更新了)我把它替换成了fluidsynth. import java.awt.BorderLayout; import javax.swing.JButton; class Main { public static void main(final String[] args) { // TODO 自动生成的方法存根 final GridLayout GL = new GridLayout(); final BorderLayout BL = new BorderLayout(); final JFrame JF = new JFrame("Jmidilame"); final JButton JB = new JButton("开始"); JB.setEnabled(false); final JButton JB2 = new JButton("浏览"); final JButton JB3 = new JButton("浏览"); final JScrollPane JSP = new JScrollPane(); final JTextArea JTA = new JTextArea(); JTA.setEditable(false); JTA.setLineWrap(true); JSP.setViewportView(JTA); final JLabel JL = new JLabel("待转换的midi文件:"); final JTextField JTF = new JTextField(); JTF.setEditable(false); final JLabel JL2 = new JLabel("转换后的mp3保存路径:"); final JTextField JTF2 = new JTextField(); JTF2.setEditable(false); final JPanel JP = new JPanel(); final JPanel JP2 = new JPanel(); final JPanel JP3 = new JPanel(); JF.setUndecorated(true); JF.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); JF.setSize(1050,1050); JF.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE); JF.setLocationRelativeTo(null); JF.setLayout(BL); JP.setLayout(GL); JP2.setLayout(GL); JP3.setLayout(GL); JF.add(JP,BorderLayout.NORTH); JF.add(JP2,BorderLayout.CENTER); JF.add(JP3,BorderLayout.SOUTH); JP.add(JL); JP.add(JTF); JP.add(JB2); JP2.add(JSP); JP2.add(JB); JP3.add(JL2); JP3.add(JTF2); JP3.add(JB3); JF.setVisible(true); JB2.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent arg0) { // TODO 自动生成的方法存根 final JFileChooser JFC = new JFileChooser(); JFC.setAcceptAllFileFilterUsed(false); JFC.setMultiSelectionEnabled(false); final JFrame JF2 = new JFrame(JF.getTitle() + " - " + "选择midi文件"); JF2.setUndecorated(true); JF2.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG); JF2.setSize(575,575); JF2.setLocationRelativeTo(null); JF2.setDefaultCloSEOperation(WindowConstants.DISPOSE_ON_CLOSE); JF2.add(JFC); JF2.setVisible(true); JFC.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent arg0) { // TODO 自动生成的方法存根 if (arg0.getActionCommand() == JFileChooser.CANCEL_SELECTION) { JF2.dispose(); } if (arg0.getActionCommand() == JFileChooser.APPROVE_SELECTION) { JF2.dispose(); final File F = JFC.getSelectedFile(); JTF.setText(F.getAbsolutePath()); } } }); } }); JB3.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent arg0) { // TODO 自动生成的方法存根 final JFileChooser JFC2 = new JFileChooser(); JFC2.setAcceptAllFileFilterUsed(false); JFC2.setMultiSelectionEnabled(false); JFC2.setDialogType(JFileChooser.SAVE_DIALOG); final JFrame JF3 = new JFrame(JF.getTitle() + " - " + "选择mp3文件的保存位置"); JF3.setUndecorated(true); JF3.getRootPane().setWindowDecorationStyle(JRootPane.FILE_CHOOSER_DIALOG); JF3.setSize(575,575); JF3.setLocationRelativeTo(null); JF3.setDefaultCloSEOperation(WindowConstants.DISPOSE_ON_CLOSE); JF3.add(JFC2); JF3.setVisible(true); JFC2.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent arg0) { // TODO 自动生成的方法存根 if (arg0.getActionCommand() == JFileChooser.CANCEL_SELECTION) { JF3.dispose(); } if (arg0.getActionCommand() == JFileChooser.APPROVE_SELECTION) { JF3.dispose(); final File F2 = JFC2.getSelectedFile(); JTF2.setText(F2.getAbsolutePath()); if (JTF.getText() != null && JTF2.getText() != null) { JB.setEnabled(true); } } } }); } }); JB.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent arg0) { // TODO 自动生成的方法存根 new Thread(new Runnable() { @Override public void run() { // TODO 自动生成的方法存根 JB.setEnabled(false); JB2.setEnabled(false); JB3.setEnabled(false); JF.setDefaultCloSEOperation(WindowConstants.DO_NOTHING_ON_CLOSE); JTA.setText(null); try { final Process P = Runtime.getRuntime().exec( "fluidsynth -v -F /root/output.flac -T flac /usr/share/sounds/sf2/FluidR3_GM.sf2 " // "-v"= "--verbose",显示完整的执行进度。 + JTF.getText()); final InputStreamReader ISR = new InputStreamReader(P.getErrorStream()); final BufferedReader BR = new BufferedReader(ISR); String s; while ((s = BR.readLine()) != null) { JTA.append(s += ‘n‘); JTA.setCaretPosition(JTA.getDocument().getLength()); } BR.close(); ISR.close(); final Process P2 = Runtime.getRuntime().exec( "ffmpeg -vol 448 -stats -loglevel level+verbose -y -i /root/output.flac -f mp3 -ab 320k -ac 2 -ar 48000 " // ”-loglevel level+verbose -stats“这两个参数可以显示完整的转换进度。 + JTF2.getText()); final InputStreamReader ISR2 = new InputStreamReader(P2.getErrorStream()); final BufferedReader BR2 = new BufferedReader(ISR2); String s2; while ((s2 = BR2.readLine()) != null) { JTA.append(s2 += ‘n‘); JTA.setCaretPosition(JTA.getDocument().getLength()); } BR2.close(); ISR2.close(); JB.setEnabled(true); JB2.setEnabled(true); JB3.setEnabled(true); JF.setDefaultCloSEOperation(WindowConstants.EXIT_ON_CLOSE); Runtime.getRuntime().exec("rm -f /root/output.flac"); } catch (final IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }).start(); } }); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |