给自己的软件加上背景音乐

dwzh20 2004-12-16 08:31:12
请问如何做。
...全文
104 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lEFTmOON 2004-12-16
  • 打赏
  • 举报
回复
package kyodai;

import javax.sound.midi.*;
import java.io.*;
import java.net.*;

/**
* <p>Title: LianLianKan</p>
* <p>Description: 连连看</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: www.wuhantech.com</p>
* @author ZhangJian
* @version 1.0
*/

public class Music
implements MetaEventListener, Runnable {
private String midiFile = "sound/bg.mid";
private Sequence sequence = null;
private Sequencer sequencer;
private boolean isPlaying = false;
private volatile Thread thread;

public Music() {
try {
loadMidi(midiFile);
}
catch (InvalidMidiDataException ex) {
}
catch (IOException ex) {
}
}

/**
* 读取midi文件
* @param filename
*/
public void loadMidi(String filename) throws IOException,
InvalidMidiDataException {
URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL url = urlLoader.findResource(filename);
sequence = MidiSystem.getSequence(url);
}

public void play() {
if (isPlaying) { //如果已经在播放,返回
return;
}

try {
// Create a sequencer for the sequence
sequencer = MidiSystem.getSequencer();
sequencer.open();
sequencer.setSequence(sequence);

sequencer.addMetaEventListener(this);
}
catch (InvalidMidiDataException ex) {
}
catch (MidiUnavailableException e) {
}

// Start playing
thread = new Thread(this);
thread.start();
}

public void stop() {
if (isPlaying) {
sequencer.stop();
isPlaying = false;
}
if (thread != null) {
thread = null;
}
}

public void run() {
Thread currentThread = Thread.currentThread();
while (currentThread == thread && !isPlaying) {
sequencer.start();
isPlaying = true;
try {
thread.sleep(1000l);
}
catch (InterruptedException ex) {
}
}
}

public void meta(MetaMessage event) {
if (event.getType() == 47) {
System.out.println("Sequencer is done playing.");
}
}
}
Eraserpro 2004-12-16
  • 打赏
  • 举报
回复
先import sun.audio.AudioPlayer;
然后:
AudioPlayer ap = AudioPlayer.player;
try {
ap.start(new FileInputStream(new File("c:\\ohmygod.wav")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
thomas_20 2004-12-16
  • 打赏
  • 举报
回复
关注……,顶
射天狼 2004-12-16
  • 打赏
  • 举报
回复
/*
*文件名:PlaySound.java
*功能用途:APPLET小应用程序, 播放声音
*创建人:崔占民
*创建日期:2004/08/03
*修改人:
*修改日期:
*版权:
*函数说明:
*/

/**
* @author 崔占民
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
package PlayMedia;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.applet.AudioClip;

public class PlayMedia extends Applet
{
Image testImage;
AudioClip audio;

public void init ()
{
Button btnPlay = new Button ("Play");
Button btnStop = new Button ("Stop");
add (btnPlay);
add (btnStop);

try
{
testImage = getImage (getDocumentBase (), "house2.gif");
audio = getAudioClip (getDocumentBase (), "onestop.mid");
}
catch (Exception e)
{
System.out.println (e.toString ());
}

btnPlay.addActionListener(new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
try
{
audio.play ();
}
catch (Exception err)
{
System.out.println (err.toString ());
}
}
});

btnStop.addActionListener (new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
try
{
audio.stop ();
}
catch (Exception err)
{
System.out.println (err.toString ());
}
finally
{

}
}
});
}

public void paint (Graphics g)
{
g.drawImage (testImage, 0, 0, this);
}
// public boolean mouseDown (Event evt, int x, int y)
// {
// audio.play ();
// System.out.println("dddd");
// return true;
// }
}
不徻写代码 2004-12-16
  • 打赏
  • 举报
回复
有这方面的API
j2nix 2004-12-16
  • 打赏
  • 举报
回复
楼主直接问问《java 日记本》的作者胡映:whhuying@163.com

他做的那个日记本就是有背景音乐的,还可以控制。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧