📄 sound.java~6~
字号:
package mymatrix;
import java.io.*;
import javax.sound.sampled.*;
public class Sound {
String m_SoundName;
Clip clip;
public Sound(String SoundName) {
m_SoundName=SoundName;
jsInit();
}
public Sound(String name,int i){
PlayMp3(name);
}
private void jsInit(){
try{
File file = new File(m_SoundName);
AudioInputStream stream = AudioSystem.getAudioInputStream(file);
AudioFormat format = stream.getFormat();
// specify what kind of line we want to create
DataLine.Info info = new DataLine.Info(Clip.class, format);
// create the line
clip = (Clip)AudioSystem.getLine(info);
// load the samples from the stream
clip.open(stream);
}
catch(Exception se){
se.printStackTrace();
}
}
public void PlayMp3(String name){
// create the format used for playback
// (uncompressed, 44100Hz, 16-bit, mono, signed, little-endian)
AudioFormat playbackFormat =
new AudioFormat(44100, 16, 1, true, false);
// open the source file
try{
AudioInputStream source =
AudioSystem.getAudioInputStream(new File(name));
// convert to playback format
source = AudioSystem.getAudioInputStream(playbackFormat, source);
AudioFormat format = source.getFormat();
// specify what kind of line we want to create
DataLine.Info info = new DataLine.Info(Clip.class, format);
// create the line
clip = (Clip)AudioSystem.getLine(info);
// load the samples from the stream
clip.open(source);
clip.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void start(){
// begin playback of the sound clip
clip.start();
}
public void stop(){
clip.stop();
}
public void loop(int LoopTime){
clip.loop(LoopTime);
}
public void pause(){
try{
clip.wait();
}
catch(Exception pe){
pe.printStackTrace();
}
}
public void continue_play(){
if(!clip.isActive())
clip.notify();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -