java中播放声音(一)
2007-06-02 22:26
302 查看
此代码可以打开AudioFormat 为PCM_SIGNED, 11025.0 Hz, 16 bit的wav后缀文件,大家可以参考以下代码
import javax.sound.sampled.*;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
class SecondTest {
public static void loadSound() {
try {
String audiofile= "c://audio//system.wav";
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(
audiofile));
// From URL
// AudioInputStream stream = AudioSystem.getAudioInputStream(new URL(
// "http://192.168.254.102:8888/oaapp/1-welcome.wav"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
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 the clip
DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
((int) stream.getFrameLength() * format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is completely loaded
clip.open(stream);
// Start playing
clip.start();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} catch (LineUnavailableException e) {e.printStackTrace();
} catch (UnsupportedAudioFileException e) {e.printStackTrace();
}
}
public static void main(String args[]) {
loadSound();
}
}
import javax.sound.sampled.*;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.io.IOException;
class SecondTest {
public static void loadSound() {
try {
String audiofile= "c://audio//system.wav";
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File(
audiofile));
// From URL
// AudioInputStream stream = AudioSystem.getAudioInputStream(new URL(
// "http://192.168.254.102:8888/oaapp/1-welcome.wav"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
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 the clip
DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat(),
((int) stream.getFrameLength() * format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is completely loaded
clip.open(stream);
// Start playing
clip.start();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {e.printStackTrace();
} catch (LineUnavailableException e) {e.printStackTrace();
} catch (UnsupportedAudioFileException e) {e.printStackTrace();
}
}
public static void main(String args[]) {
loadSound();
}
}
相关文章推荐
- java 播放 wav 声音文件
- 在 java 中播放声音(2)
- java获取声音与播放声音
- Java播放声音文件
- Java播放声音文件
- java中播放声音(一)
- java swing 播放声音
- 完美解决Java程序在 MOTO E680i 中声音文件播放
- 【利用Java Applet播放声音文件】
- 解决Java程序在MOTO E680i中声音文件播放
- java播放声音
- Java Applet应用:显示图片、播放声音
- 在 JAVA 中播放声音(1)
- Java播放声音的几种方式
- Java写的播放声音的程序
- java 播放 wav格式的声音
- java播放声音
- windows播放wav声音文件--java播放wav完整demo
- java应用程序中的声音播放
- 解决H5播放视频黑屏只有声音没有图像的问题,Java调用ffmpeg转码成h264的mp4格式