繁体   English   中英

剪辑没有播放任何声音

[英]Clip not playing any sound

好吧,标题说明了一切,我尝试使用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();

}

如前所述,您需要防止主线程退出,因为这将导致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("...");
    }
}

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

Java的声音Clip需要一个活动的Thread才能播放音频输入文件,否则应用程序将退出,然后才能播放该文件。 您可以添加

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

调用start

播放音频剪辑的正确方法是排空线路,如播放音频中所述

因此您的主要对象应如下所示:

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()阻塞,直到播放play.drain()所有数据。

在这里播放剪辑

    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!");
                }
            });
        }

您的程序在声音播放之前就终止了。 我会玩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");
        }
    });

}

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

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

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

  1. src文件夹中创建一个文件夹,例如:“ music”,然后在其中放置音频文件
  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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM