JAVA使用Clip类播放声音

	String FilePath="C:\\Users\\sunling\\Documents\\Killercars's Files\\JavaWorkspace\\ChordRepeat";
	try{
		File file = new File(FilePath+"\\4250.wav");
		AudioInputStream audioIn = AudioSystem.getAudioInputStream(file);  
		Clip clip = AudioSystem.getClip();
		clip.open(audioIn);
		clip.loop(Clip.LOOP_CONTINUOUSLY);//循环播放 其他 可用方法有start()和stop()
	} catch (UnsupportedAudioFileException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (LineUnavailableException e) {  
            e.printStackTrace();  
        } 

代码很短,调试发现没有抛出异常但是就是没有声音(.....

解决方法为在末尾加上 Thread.sleep(10000);

实际使用不会有这种问题

另外 用 start()单次播放不是play().......很多介绍没注意写的是play() 根本没这个方法

猜你喜欢

转载自blog.csdn.net/Excaliburrr/article/details/54576468
今日推荐