我随便弄了一个程序,求大神帮忙给加一个背景音乐,最好是不需要按钮,当界面打开的时候就自动播放的,当然有按钮也可以,谢谢啦
1 package dsa; 2 import java.awt.*; 3 4 import javax.swing.*; 5 6 7 public class tianjiabeijingyinyue extends JFrame 8 { 9 private JLabel label1=new JLabel("添加背景音乐"); 10 public tianjiabeijingyinyue() 11 { 12 super("添加背景音乐"); 13 this.add(label1,BorderLayout.NORTH); 14 this.setSize(300, 300); 15 this.setBackground(Color.lightGray); 16 this.setDefaultCloseOperation(EXIT_ON_CLOSE); 17 18 this.setVisible(true); 19 20 } 21 22 public static void main(String arg[]) 23 { 24 new tianjiabeijingyinyue(); 25 26 } 27 28 }
import javax.sound.sampled.*;
import java.io.*;
public class TestMusic{
private AudioFormat format;
private byte[] samples;
public static void main(String args[])throws Exception{
TestMusic sound =new TestMusic("1.wav");
InputStream stream =new ByteArrayInputStream(sound.getSamples());
// play the sound
sound.play(stream);
// exit
System.exit(0);
}
public TestMusic(String filename) {
try {
// open the audio input stream
AudioInputStream stream =AudioSystem.getAudioInputStream(new File(filename));
format = stream.getFormat();
// get the audio samples
samples = getSamples(stream);
}
catch (UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
public byte[] getSamples() {
return samples;
}
private byte[] getSamples(AudioInputStream audioStream) {
// get the number of bytes to read
int length = (int)(audioStream.getFrameLength() * format.getFrameSize());
// read the entire stream
byte[] samples = new byte[length];
DataInputStream is = new DataInputStream(audioStream);
try {
is.readFully(samples);
}
catch (IOException ex) {
ex.printStackTrace();
}
// return the samples
return samples;
}
public void play(InputStream source) {
// use a short, 100ms (1/10th sec) buffer for real-time
// change to the sound stream
int bufferSize = format.getFrameSize() *
Math.round(format.getSampleRate() / 10);
byte[] buffer = new byte[bufferSize];
// create a line to play to
SourceDataLine line;
try {
DataLine.Info info =
new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine)AudioSystem.getLine(info);
line.open(format, bufferSize);
}
catch (LineUnavailableException ex) {
ex.printStackTrace();
return;
}
// start the line
line.start();
// copy data to the line
try {
int numBytesRead = 0;
while (numBytesRead != -1) {
numBytesRead =
source.read(buffer, 0, buffer.length);
if (numBytesRead != -1) {
line.write(buffer, 0, numBytesRead);
}
}
}
catch (IOException ex) {
ex.printStackTrace();
}
// wait until all data is played, then close the line
line.drain();
line.close();
}
}
web上添加背景音乐?
audio
embed?
个人感觉还是在web 页面添加背景音乐好. 路过学习..
这个要用到audio包的代码好像。。。
import java.applet.Applet; import java.applet.AudioClip; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.swing.JTextField; public class Shengyin { int g; String b; AudioClip aau1; JTextField ss; public Shengyin(){ } public Shengyin(JTextField a){ ss=a;b=ss.getText().trim();System.out.println(b); } public void a(JTextField a){ try { ss=a;b=ss.getText().trim();System.out.println(b); URL cb1; File f1 = new File("sound/"+b+".wav"); System.out.println("zhun"+b+""); cb1 = f1.toURL(); aau1 = Applet.newAudioClip(cb1); aau1.play();//循环播放 aau.loop() 单曲 aau.stop()停止播放 g=1; } catch (MalformedURLException e) { e.printStackTrace(); } } }