剪辑不打任何声音 [英] Clip not playing any sound
问题描述
那么,标题说的是,我尝试使用javax.sound并没有什么玩的wav文件正在发生的事情。我已经尝试了许多不同的文件没有任何运气。
公共静态无效的主要(字串[] args)抛出IOException异常,UnsupportedAudioFileException,LineUnavailableException
{ 文件=新的文件(C:\\\\ \\\\用户\\\\桑德拉桌面\\\\ \\\\音乐rags.wav);
的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(中);
剪辑播放= AudioSystem.getClip();
play.open(的AudioInputStream);
FloatControl体积=(FloatControl)play.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1.0F); // 10分贝降低音量。
play.start();}
作为,已经开始所说,你需要prevent从退出主线程,因为这将导致JVM终止。
剪辑#启动
不是一个阻塞调用,这意味着它将返回(即将)它被称为后。
我毫不怀疑,有很多方法可以解决这个问题,这是其中之一,只是一个简单的例子。
公共类PlayMusic { 公共静态无效的主要(字串[] args)抛出InterruptedException的{
剪辑播放= NULL;
尝试{
文件=新的文件(C:\\\\ \\\\用户\\\\公共音乐\\\\示例音乐\\\\ Kalimba.wav);
的AudioInputStream的AudioInputStream = AudioSystem.getAudioInputStream(中);
玩= AudioSystem.getClip();
play.open(的AudioInputStream);
FloatControl体积=(FloatControl)play.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1.0F); // 10分贝降低音量。
play.start();
//循环直到剪辑没有不再运行。
//我们循环这种方式使线路填补,否则isRunning意志
//返回false
// {做
//视频下载(15);
//}而(play.isRunning());
play.drain();
}赶上(UnsupportedAudioFileException | IOException异常| LineUnavailableException前){
ex.printStackTrace();
} {最后
尝试{
play.close();
}赶上(例外EXP){
}
}
的System.out.println(...);
}
}
在实际的解决方案将取决于您的需求。
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();
}
As, has already begin stated, you need to prevent the main thread from exiting, as this will cause the JVM to terminate.
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屋!