java按键发出声音代码_java关于播放声音的代码的解释

publicstaticvoidplayCycle(Strings){//重复运行。Stringfileurl="src/sound/"+s;try{AudioInputStreamais=AudioSystem.getAudioInputStream(newFile(fileurl));AudioFormataif=ais.g...

public static void playCycle(String s){//重复运行。

String fileurl = "src/sound/" + s;

try{

AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileurl));

AudioFormat aif = ais.getFormat();

SourceDataLine sdl = null;

DataLine.Info info = new DataLine.Info(SourceDataLine.class,aif);

sdl = (SourceDataLine)AudioSystem.getLine(info);

sdl.open(aif);

sdl.start();

//play

int nByte = 0;

byte[] buffer = new byte[128];

while(nByte != -1){

nByte = ais.read(buffer,0,128);

if(nByte >= 0){

int oByte = sdl.write(buffer, 0, nByte);

//System.out.println(oByte);

}

}

sdl.stop();

playCycle(s);

}catch(UnsupportedAudioFileException e){

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (LineUnavailableException e) {

e.printStackTrace();

}

}

public static void play(String s){//只运行一遍

String fileurl = "src/sound/" + s;

try{

AudioInputStream ais = AudioSystem.getAudioInputStream(new File(fileurl));

AudioFormat aif = ais.getFormat();

SourceDataLine sdl = null;

DataLine.Info info = new DataLine.Info(SourceDataLine.class,aif);

sdl = (SourceDataLine)AudioSystem.getLine(info);

sdl.open(aif);

sdl.start();

//play

int nByte = 0;

byte[] buffer = new byte[128];

while(nByte != -1){

nByte = ais.read(buffer,0,128);

if(nByte >= 0){

int oByte = sdl.write(buffer, 0, nByte);

//System.out.println(oByte);

}

}

sdl.stop();

}catch(UnsupportedAudioFileException e){

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (LineUnavailableException e) {

e.printStackTrace();

}

}

哪位大神给我具体解释这每行的代码,谢了~~在线等

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值