簡體   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