作了个定时器,怎么让他到时间了发声音出来啊 作了个定时器,怎么让他到时间了发声音出来啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我用了thread,都弄好了,就是不会发声音 给你一段播放声音的代码学习一下:import javax.sound.sampled.*;import java.io.*;import java.net.*; class PlaySound { public PlaySound(String pos) { try { // 创建一个你要播放的文件 AudioInputStream stream = AudioSystem.getAudioInputStream(new File(pos)); //在此改成你的文件路径 AudioFormat format = stream.getFormat(); if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { format = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED, format.getSampleRate(), format.getSampleSizeInBits()*2, format.getChannels(), format.getFrameSize()*2, format.getFrameRate(), true); // big endian stream = AudioSystem.getAudioInputStream(format, stream); } // Create line SourceDataLine.Info info = new DataLine.Info( SourceDataLine.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize())); SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(stream.getFormat()); line.start(); // Continuously read and play chunks of audio int numRead = 0; byte[] buf = new byte[line.getBufferSize()]; while ((numRead = stream.read(buf, 0, buf.length)) >= 0) { int offset = 0; while (offset < numRead) { offset += line.write(buf, offset, numRead-offset); } } line.drain(); line.stop(); stream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (LineUnavailableException e) { e.printStackTrace(); } catch (UnsupportedAudioFileException e) { e.printStackTrace(); } }} 【Java--Swing】如何画出 让一个物体飞向一个正在移动着的物体的图像出来 这样定义按钮数组有问题么? 一些有用的代码分享 栈帧 问下面代码输出结果,并解释谢谢 类加载后,对内存和jvm产生什么影响货变化? 这样的类如何编译 求,从一棵树中快速地生成一棵子树的算法 刚入门的...... 大虾,初学者该怎样学习java呀!! 为什么这个JTextPane只有这么一点大呀? 帮帮我了,我刚自学JAVA,不会,高手帮忙
import javax.sound.sampled.*;
import java.io.*;
import java.net.*;
class PlaySound {
public PlaySound(String pos) {
try {
// 创建一个你要播放的文件
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(pos)); //在此改成你的文件路径
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
} // Create line
SourceDataLine.Info info = new DataLine.Info(
SourceDataLine.class, stream.getFormat(),
((int)stream.getFrameLength()*format.getFrameSize()));
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(stream.getFormat());
line.start(); // Continuously read and play chunks of audio
int numRead = 0;
byte[] buf = new byte[line.getBufferSize()];
while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
int offset = 0;
while (offset < numRead) {
offset += line.write(buf, offset, numRead-offset);
}
}
line.drain();
line.stop();
stream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} }
}