用java 如何在游戏中插入音乐java好像只支持wav格式的音乐文件,你可以用 格式化工厂 转换一下 。。再用下面代码:
import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.URI;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class f extends JFrame implements ActionListener {
private static final String AudioClip = null;
private JButton Oj;
private JButton Oj1;
AudioClip clip =null;
File musicFile;
URI uri;
URL url;
private void f() throws InterruptedException{
musicFile = new File("/zxc.wav");
uri = musicFile.toURI();
try {
url = uri.toURL();
} catch (Exception e) {
}
clip=Applet.newAudioClip(url);
//clip.play();
}
f() {
this.setSize(800, 600);
this.setResizable(false);
JPanel p = new JPanel();
this.setContentPane(p);
this.setVisible(true);
Oj = new JButton("开始");
Oj1 = new JButton("结束");
this.setVisible(true);
Oj.addActionListener(this);
Oj1.addActionListener(this);
this.add(Oj);
this.add(Oj1);
}
public static void main(String[] args) throws InterruptedException {
f ff=new f();
ff.f();
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == Oj) {
play();
} else if (e.getSource() == Oj1) {
stop();
//System.exit(0);
}
}
public void play() {
if (clip != null)
( (java.applet.AudioClip) clip).play();
}
public void stop() {
if (clip != null)
( (java.applet.AudioClip) clip).stop();
}
}
用Java编写了个小游戏想给游戏加上背景音乐用什么方法播放音乐的代码了解一下可以直接加到小游戏里
package com.music.test;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import java.io.*;
public class Music {
private String music;
private Player player;
publicMusic() throws FileNotFoundException, JavaLayerException {
FileInputStream file = new FileInputStream("e:/闹钟.mp3");
BufferedInputStream name = new BufferedInputStream(file);
player = new Player(name);
【java游戏音乐代码 java编写音乐类】player.play();
}
public static void main(String[] args) throws FileNotFoundException, JavaLayerException{
new Music();
}
}
java做小游戏,怎么给游戏加背景音乐?? 请附上代码,希望添加背景音乐代码能简短些!谢谢 。。//主程序部分
audioPlayWave = new myMusicPlay("1.wav");// 开音乐
audioPlayWave.start();
musicOpenLab = 1;
//音乐子类(都是苦命的孩子 , 要互利互惠?。?
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class myMusicPlay extends Thread {
private String fileName;
private final int EXTERNAL_BUFFER_SIZE = 524288;
public myMusicPlay(String wavFile) {
this.fileName = wavFile;
}
public void run() {
File soundFile = new File(fileName); // 播放音乐的文件名
if (!soundFile.exists()) {
System.err.println("Wave file not found:"fileName);
return;
}
while (true) { // 设置循环播放
AudioInputStream audioInputStream = null; // 创建音频输入流对象
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile); // 创建音频对象
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat(); // 音频格式
SourceDataLine auline = null; // 源数据线
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline
.getControl(FloatControl.Type.PAN);
}
auline.start();
int nBytesRead = 0;
byte[] abData = https://www.04ip.com/post/new byte[EXTERNAL_BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream
.read(abData, 0, abData.length);
if (nBytesRead = 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
// auline.close();
}
}
}
}
java游戏音乐代码的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于java编写音乐类、java游戏音乐代码的信息别忘了在本站进行查找喔 。
推荐阅读
- ios购买记录如何删除,iphone购买记录怎么删除
- 理发店工作如何做推广,理发店工作如何做推广赚钱
- 在越南怎么做直播卖货,越南做直播的都是些什么人
- mysql主挂了怎么处理 mysql主主
- java排序代码原理,java排序算法代码实现
- wordpress本地服务器,wordpress本地搭建
- 直播伴侣声卡套装,直播伴侣怎么用声卡
- 数据库连接中php 数据库连接中止ora03135
- 硬盘上写的图片怎么删除,硬盘的照片不小心删除怎么恢复