剪辑没有播放任何声音 [英] Clip not playing any sound

查看:34
本文介绍了剪辑没有播放任何声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,标题说明了一切,我尝试使用 javax.sound 播放 wav 文件,但没有任何反应.我尝试了许多不同的文件,但没有任何运气.

Well, the title says all, I tried playing a wav file using javax.sound and nothing is happening. I have tried many different files without any luck.

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 终止.

As, has already begin stated, you need to prevent the main thread from exiting, as this will cause the JVM to terminate.

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

Clip#start is not a blocking call, meaning that it will return (soon) after it is called.

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

I have no doubt that there are many ways to approach this problem and this is just a simple example of one of them.

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

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

The actual solution will depend on your needs.

这篇关于剪辑没有播放任何声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆