剪辑没有播放任何声音

时间:2013-06-16 23:15:05

标签: java audio javasound

嗯,标题说的全部,我尝试使用javax.sound播放一个wav文件,但没有任何事情发生。我没有运气就试过很多不同的文件。

public static void main(String[] args) throws IOException, UnsupportedAudioFileException, LineUnavailableException
{

    File in = new File("C:\\Users\\Sandra\\Desktop\\music\\rags.wav");
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
    Clip play = AudioSystem.getClip();
    play.open(audioInputStream);
    FloatControl volume= (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN);
    volume.setValue(1.0f); // Reduce volume by 10 decibels.
    play.start();

}

6 个答案:

答案 0 :(得分:7)

因为,已经开始声明,你需要阻止主线程退出,因为这会导致JVM终止。

Clip#start不是阻塞调用,这意味着它会在调用后很快返回。

我毫不怀疑有很多方法可以解决这个问题,这只是其中一个问题的一个简单例子。

public class PlayMusic {

    public static void main(String[] args) throws InterruptedException {
        Clip play = null;
        try {
            File in = new File("C:\\Users\\Public\\Music\\Sample Music\\Kalimba.wav");
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
            play = AudioSystem.getClip();
            play.open(audioInputStream);
            FloatControl volume = (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN);
            volume.setValue(1.0f); // Reduce volume by 10 decibels.
            play.start();
            // Loop until the Clip is not longer running.
            // We loop this way to allow the line to fill, otherwise isRunning will
            // return false
            //do {
            //    Thread.sleep(15);
            //} while (play.isRunning());
            play.drain();
        } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
            ex.printStackTrace();
        } finally {
            try {
                play.close();
            } catch (Exception exp) {
            }
        }
        System.out.println("...");
    }
}

实际的解决方案取决于您的需求。

答案 1 :(得分:4)

Java的声音Clip需要有效Thread来播放音频输入文件,否则应用程序会在播放文件之前退出。你可以添加

JOptionPane.showMessageDialog(null, "Click OK to stop music");

致电start

答案 2 :(得分:3)

播放音频片段的正确方法是按Playing Back Audio

中的说明排空线路

所以你的主要应该是这样的:

public static void main(String[] args) throws IOException,
    UnsupportedAudioFileException, LineUnavailableException
{
    File in = new File("C:\\Users\\Sandra\\Desktop\\music\\rags.wav");
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
    Clip play = AudioSystem.getClip();
    play.open(audioInputStream);
    FloatControl volume= (FloatControl) play.getControl(FloatControl.Type.MASTER_GAIN);
    volume.setValue(1.0f); // Reduce volume by 10 decibels.
    play.start();
    play.drain();
    play.close();
}

play.drain()阻止,直到播放完所有数据。

答案 3 :(得分:2)

此处playing a clip

    public static void main(String[] args) throws Exception {
           File in = new File("C:\\Users\\Sandra\\Desktop\\music\\rags.wav");
           AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(in);
           Clip play = AudioSystem.getClip();
           play.open(audioInputStream);
           FloatControl volume= (FloatControl)play.getControl(FloatControl.Type.MASTER_GAIN);
           volume.setValue(1.0f); // Reduce volume by 10 decibels.
           play.start();
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    // A GUI element to prevent the Clip's daemon Thread
                    // from terminating at the end of the main()
                    JOptionPane.showMessageDialog(null, "Close to exit!");
                }
            });
        }

答案 4 :(得分:0)

您的节目在声音有时间播放之前终止。我会以某种线程方式(play.start();,...)进行invokeLater,并找到一种等待声音结束的方法(Reimeus建议一个)。

领导:

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {

          ...

          play.start();
          JOptionPane.showMessageDialog(null, "Click OK to stop music");
        }
    });

}

答案 5 :(得分:0)

如果您只想播放音频,这是一种方法。

audioFileDirectory字符串变量需要正确的音频文件路径,否则将引发异常,并且程序将无法运行。

在使用IDE的项目中具有正确的音频文件目录的示例:

  1. src文件夹中创建一个文件夹,例如“音乐”,然后在其中放置音频文件
  2. audioFileDirectory = "/music/name_of_audio_file";

音频播放的重要部分是程序的主线程需要以某种方式保持“活跃”状态,因此该行

Thread.sleep(audio.getMicrosecondLength()/1000);

是主线程处于“活动”状态的位置,参数audio.getMicrosecondLength()/1000是将处于“活动”状态的时间,即音频文件的整个长度。

public class AudioTest
{
    void playAudio() throws Exception
    {
        String audioFileDirectory = "your_audioFileDirectory";
        InputStream is = getClass().getResourceAsStream(audioFileDirectory);
        BufferedInputStream bis = new BufferedInputStream(is);
        AudioInputStream ais = AudioSystem.getAudioInputStream(bis);
        Clip audio = AudioSystem.getClip();
        audio.open(ais);
        audio.loop(Clip.LOOP_CONTINUOUSLY);
        audio.start();
        Thread.sleep(audio.getMicrosecondLength()/1000);
        audio.close();
    } // end playAudio

    public static void main(String[] args) 
    {
        try 
        {
            new AudioTest().playAudio();
        } 
        catch (Exception e) 
        {
            System.out.println("Class: " + e.getClass().getName());
            System.out.println("\nMessage:\n" + e.getMessage() + "\n");
            e.printStackTrace();
        }
    } // end main
} // end class AudioTest
相关问题