首页 新闻 会员 周边 捐助

如何在java的界面中添加背景音乐?急啊!!!!

0
悬赏园豆:5 [已解决问题] 解决于 2015-05-11 17:53

我随便弄了一个程序,求大神帮忙给加一个背景音乐,最好是不需要按钮,当界面打开的时候就自动播放的,当然有按钮也可以,谢谢啦

 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 }
左手、右手的主页 左手、右手 | 初学一级 | 园豆:78
提问于:2013-01-02 16:05
< >
分享
最佳答案
-1


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();
}

}

收获园豆:4
明明小k | 菜鸟二级 |园豆:246 | 2013-05-31 18:18
其他回答(3)
0

web上添加背景音乐?

audio  

embed?

收获园豆:1
chenping2008 | 园豆:9836 (大侠五级) | 2013-01-04 09:49
0

个人感觉还是在web 页面添加背景音乐好. 路过学习..

龙尹 | 园豆:132 (初学一级) | 2013-01-04 11:45
0

这个要用到audio包的代码好像。。。

羽商宫 | 园豆:2490 (老鸟四级) | 2013-01-04 13:48
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();
          }

         }
}
支持(0) 反对(0) 羽商宫 | 园豆:2490 (老鸟四级) | 2013-01-04 13:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册