当前位置: 移动技术网 > 移动技术>移动开发>IOS > 五子棋的彩蛋——背景音效

五子棋的彩蛋——背景音效

2020年07月13日  | 移动技术网移动技术  | 我要评论

五子棋的彩蛋

       ~~~~~~~前面已经把五子棋的主体写好了,但是还比较单调无趣。因此,本节我们给五子棋加上背景音效。
       ~~~~~~~

AudioInputStream as;


//打开音频文件(wav格式) 
as = AudioSystem.getAudioInputStream(new File("src/WZQ0630v13/backmusic.wav");

AudioFormat format = as.getFormat();
SourceDataLine sdl = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
	sdl = (SourceDataLine) AudioSystem.getLine(info);
	sdl.open(format);
} catch (LineUnavailableException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

sdl.start();
int nBytesRead = 0;
byte[] abData = new byte[512];
while (nBytesRead != -1) {
	nBytesRead = as.read(abData, 0, abData.length);
	if (nBytesRead >= 0)
		sdl.write(abData, 0, nBytesRead);
}
//关闭SourceDataLine
sdl.drain();
sdl.close();


为了让效果更好,这里最好使用线程使它休眠。

public void run(){
 for(int i=0;i<400;i++){
     g.fillOval(x+i*2,y,30,30);
  //暂停300毫秒
     try{
     Thread.sleep(30);//
     }catch(Exception ef){} 
	} 
}

如果对线程有兴趣的,可前往Java中线程的休眠(sleep方法)学习。

本文地址:https://blog.csdn.net/IDNIHAI/article/details/107286438

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网