java 窗口怎么加背景音乐?我已经写好窗口了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 窗口怎么加背景音乐?我已经写好窗口了相关的知识,希望对你有一定的参考价值。

你好!很高兴为你解答。
首先,你打开这个登录窗口之后,你要求它能够播放音乐,你就在你启动的JFrame主线程main构造并显示完成JFrame后,新建一个方法叫做this.playBgMusic();

这里解释一下,为什么叫你在启动JFrame主线程中调用这个方法而不是在你的QQLoinFrame的构造方法中调用,原因是Swing的处理机制原理是利用EventQueue来执行的,可以说,如果你直接new的话都是单线程(main)处理程序,这样的话就会对这个线程依赖过大,而这个线程也负荷过大。当你想启动播放音乐或者IO读写之类的耗时线程时,你可以试试在构造方法中执行这方法,你将会发现你的Swing程序没反应,要等某线程处理完后才可以继续显示。这里main线程阻塞,所以你的程序表面看起来不动,组件没反应,这就是因为阻塞掉了。解决这个问题的办法就是如下(这是其中一种,另一种你可以自行查找):
public static void main(String args[])
java.awt.EventQueue.invokeLater(new Runnable()

public void run()
QQLogin qq = new QQLogin();
this.playBgMusic();

);

*********************************************正文********************************************************
假设,你启动JFrame的主方法如下:
public static void main(String[] args)
QQLogin qq = new QQLogin();
this.playBgMusic();


*******************************************this.playBgMusic()方法*********************************************
/**
*播放背景音乐,调用PlaySound线程类
/
public void playBgMusic()
PlaySound play = new PlaySound();
Thread t = new Thread(play);
t.start();


*******************************************PlaySound线程类********************************************

/**
*PlaySound线程类,其中,因为你要求不使用JMF,所以,请选择.au或者.wav格式吧
/
public class PlaySound implements Runnable

public void run()
try
AudioClip audio;
URL url = null;
File musicFile = new File("msg.wav");
URI uri = musicFile.toURI();
url = uri.toURL();
audio = Applet.newAudioClip(url);
audio.play();
catch (MalformedURLException ex)
Logger.getLogger(PlayMsgSound.class.getName()).log(Level.SEVERE, null, ex);



参考技术A 其实就是写一条线程去播放指定的音频而已,至于java怎么播放音频,播放啥格式的音,这个你到网上搜一下,那个条播放的线程就提供些什么播放,暂停,停止的方法给主线程调用 参考技术B java只能播放wav格式音频文件,其它格式要下载解码器。

/**
*
* @param fileName wav格式音频文件
* @throws UnsupportedAudioFileException
* @throws IOException
* @throws LineUnavailableException
*/
public static void wavSoundPlayer(String fileName) throws UnsupportedAudioFileException, IOException, LineUnavailableException
File f = new File(fileName);
if (!f.exists())
return;


// From file
AudioInputStream stream = Audiosystem.getAudioInputStream(f);
try
// 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(),
16,
format.getChannels(),
format.getChannels() * 2,
format.getSampleRate(),
false); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);


// Create the clip
DataLine.Info info = new DataLine.Info(SourceDataLine.class, stream.getFormat(), AudioSystem.NOT_SPECIFIED);
SourceDataLine m_line = (SourceDataLine) AudioSystem.getLine(info);
m_line.open(stream.getFormat(), m_line.getBufferSize());
m_line.start();

int numRead = 0;
byte[] buf = new byte[m_line.getBufferSize()];
while ((numRead = stream.read(buf, 0, buf.length)) >= 0)
int offset = 0;
while (offset < numRead)
offset += m_line.write(buf, offset, numRead - offset);


m_line.drain();
m_line.stop();
m_line.close();
finally
stream.close();

参考技术C AudioClip audio=JApplet.newAudioClip(URL url);
audio.play();//播放音乐
方法是java提供的一种简单读取音频的一个api方法,
不过只支持ogg. au ,mid,等格式的简单音频,你可以试试看。
参考技术D 下面是一个JAVA音乐播放小程序,可以参考下:
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import java.net.*;

public class Yinyue implements ActionListener
JMenuItem menuItem1,menuItem2,menuItem3,menuItem4; //4个菜单命令

AudioClip sound=loadSound("1.wav");//变量 sound 保存音频文件 1.wav

JTextArea output;
static JFrame frame;

public static void main(String args[])
frame=new JFrame("播放音乐");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Yinyue yin=new Yinyue();
frame.setJMenuBar(yin.createMenuBar());
frame.setContentPane(yin.createContentPane());
frame.setSize(200,150);
frame.setVisible(true);

public JMenuBar createMenuBar()
JMenuBar menuBar=new JMenuBar(); //创建 JMenuBar 对象
JMenu menu=new JMenu("音乐"); //创建 JMenu 对象(主菜单)
menuItem1=new JMenuItem("播放"); //创建菜单命令
menu.add(menuItem1); //将菜单命令添加到对应的主菜单中
menuItem2=new JMenuItem("循环");
menu.add(menuItem2);
menuItem3=new JMenuItem("停止");
menu.add(menuItem3);
menuItem4=new JMenuItem("退出");
menu.add(menuItem4);
menuBar.add(menu);
//将主菜单及其菜单命令添加到 JMenuBar 对象中
menuItem1.addActionListener(this);
menuItem2.addActionListener(this);
menuItem3.addActionListener(this);
menuItem4.addActionListener(this);
return menuBar; //返回 JMenuBar 对象

public JPanel createContentPane()
JPanel contentPane=new JPanel(new BorderLayout());
output=new JTextArea(5,30);
output.setEditable(false);
contentPane.add(output,BorderLayout.CENTER);
return contentPane;

/*根据用户单击的菜单命令,执行相应的语句*/
public void actionPerformed(ActionEvent e)
if(e.getSource()==menuItem1) //播放音频文件
sound.play();
output.setText("播放音频文件");

if(e.getSource()==menuItem2) //循环播放音频文件
sound.loop();
output.setText("循环播放音频文件");

if(e.getSource()==menuItem3) //停止播放音频文件
sound.stop();
output.setText("停止播放音频文件");

if(e.getSource()==menuItem4)
int n=JOptionPane.showConfirmDialog(frame, "是否关闭程序","播放音乐",
JOptionPane.OK_CANCEL_OPTION);
if(n==JOptionPane.OK_OPTION)System.exit(0);
//退出程序


/*导入音频文件*/
private AudioClip loadSound(String fileName)
URL url=null; //保存音频文件的完整路径
try
url=new URL("file:"+System.getProperty("user.dir")+"/"+fileName);

catch(MalformedURLException e)
return Applet.newAudioClip(url); //返回创建的 AudioClip 对象



ps:java支持的音乐格式主要有: au ,wav,midi,mid,aiff,不支持MP3。

以上是关于java 窗口怎么加背景音乐?我已经写好窗口了的主要内容,如果未能解决你的问题,请参考以下文章

如何在java的Frame窗口里加入背景音乐?具体怎么在代码里添加

java界面的背景图片怎么加,如果把图片加在JLabel上填满面板那怎么样才能再加button?

java窗口背景颜色怎么设定?用setBackground()好像不行,请大侠指教!

java窗口的背景颜色

用netbeans将布局、组件之类的拖在框架中,也写好界面了,请问怎么添加背景图片呢?急~~~谢谢!

如何向java窗体中添加背景图片