【问题标题】:Playing audio in Java [duplicate]用Java播放音频[重复]
【发布时间】:2014-11-19 00:02:46
【问题描述】:

我正在尝试制作一个音板程序,将声音加载到按钮中,然后在按下按钮时播放声音。这就是我目前所拥有的 -

package soundboard;

import javax.swing.*;
import java.io.*;
import javax.sound.sampled.*;

public class Soundboard {

JButton loadButton;
JFileChooser loadBox;
JButton clearButton;
JButton button1;
JButton button2;
JButton button3;
JButton button4;
JPanel mainsPanel;

int load;
Clip clip;
File one;
File two;
File three;
File four;


public void windowCreate() {


    JFrame frame = new JFrame();
    mainsPanel = new JPanel();

    loadBox = new JFileChooser();//174
    loadBox.setSize(500,500);
    loadBox.setLocation(174,4);


    loadButton = new JButton("Load...");
    loadButton.setSize(80, 30);
    loadButton.setLocation(4, 4);
    loadButton.addActionListener(e -> {
        load = 1;
    });

    clearButton = new JButton("Clear");
    clearButton.setSize(80, 30);
    clearButton.setLocation(92, 4);
    clearButton.addActionListener(e -> {
        System.out.println("Cleared");
    });


    button1 = new JButton("1");
    button1.setSize(80, 80);
    button1.setLocation(4, 45);
    button1.addActionListener(e -> {
        if (load == 1){
            one = loadBox.getSelectedFile();
            load = 0;
        }
        else {
            System.out.println("Debugging; " + one);
            try {
                AudioInputStream audio = AudioSystem.getAudioInputStream(one);
                AudioFormat format = audio.getFormat();
                DataLine.Info info = new DataLine.Info(Clip.class, format);
                clip = (Clip) AudioSystem.getLine(info);
            }  

            catch (LineUnavailableException ex) {
                System.out.println("Err_Line");
            } 
            catch (IOException ex) {
                System.out.println("Err_IOException");
            } 
            catch (UnsupportedAudioFileException ex) {
                System.out.println("Err_FileNotSupported");
            }

            clip.setFramePosition(0);
            clip.start();
        }
    });

    button2 = new JButton("2");
    button2.setSize(80, 80);
    button2.setLocation(92, 45);
    button2.addActionListener(e -> {
        if (load == 1){
            two = loadBox.getSelectedFile();
            load = 0;
        }
        else {
            System.out.println(two);
        }
    });

    button3 = new JButton("3");
    button3.setSize(80, 80);
    button3.setLocation(4, 133);
    button3.addActionListener(e -> {
        if (load == 1){
            three = loadBox.getSelectedFile();
            load = 0;
        }
        else {

        }
    });

    button4 = new JButton("4");
    button4.setSize(80, 80);
    button4.setLocation(92, 133);
    button4.addActionListener(e -> {
        if (load == 1){
            four = loadBox.getSelectedFile();
            load = 0;
        }
        else {
            System.out.println(four);
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(loadButton);
    frame.add(clearButton);
    frame.add(button1);
    frame.add(button2);
    frame.add(button3);
    frame.add(button4);
    frame.add(loadBox);
    frame.add(mainsPanel);

    frame.setSize(675,485);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);                
}

public static void main(String[] args){
    Soundboard window = new Soundboard();
    window.windowCreate();

}


}

基本上,我需要的是在按下“button1”时播放声音的某种方式,无需打开外部窗口,无需编写新方法,也无需离开动作侦听器。将此复制粘贴到一个名为“Soundboard”的新项目中,如果您发现了这一点,请告诉我。

这是一个可以使用的测试声音 - https://drive.google.com/file/d/0B3riImENY0zSMUItWkUtaUg1X3M/edit?usp=sharing

【问题讨论】:

  • 不是重复的 - 有点不同
  • 好吧,如果您不想将其他问题的方法添加到您的代码中,您所要做的就是复制并粘贴其他代码。
  • 在我看来并不重复。相同的主题,不同的问题。
  • 我刚刚尝试从其他问题的答案中获取代码 - 它没有用。
  • “给出的代码仅适用于 MIDI 文件,..” 你怎么会认为 "http://pscode.org/media/leftright.wav" 是一个 ***ing MIDI?!?但是,如果您不相信我,请用您自己的 Wav 尝试一下。顺便说一句,我是 top answer provider for Java Sound 以及包含两个代码示例的 info. page 的作者。

标签: java swing audio directory javasound


【解决方案1】:
button1 = new JButton("1");
    button1.setSize(80, 80);
    button1.setLocation(4, 45);
    button1.addActionListener(e -> {
        if (load == 1){
            one = loadBox.getSelectedFile();
            load = 0;
        }
        else {
            System.out.println(one);
            try {
                clip = AudioSystem.getClip();
                ais1 = AudioSystem.getAudioInputStream(one);
                clip.open(ais1);
            } 
            catch (LineUnavailableException ex) {
                System.out.println("Err_Lineunavailable");
            } 
            catch (UnsupportedAudioFileException ex) {
                System.out.println("Err_FileNotSupported");
            }
            catch (IOException ex) {
                System.out.println("Err_IOException");
            }
            clip.setFramePosition(0);
            clip.start();
        }
    });

我修复了代码。变量、剪辑和所有变量都在方法之外声明为公共的,以便使它们公开。

感谢您的 cmets!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2012-04-12
    • 1970-01-01
    相关资源
    最近更新 更多