音乐剪辑java代码 java编曲( 三 )


} else {
setSize(300, 75);
setVisible(true);
return;
}
}
} }
Java怎么实现音乐播放java swt实现播放音乐代码如下:
public void play(String Filename)
{
try{
// 用输入流打开一音频文件
InputStream in = new FileInputStream(Filename);//FIlename 是你加载音乐剪辑java代码的声音文件如(“game.wav”)
// 从输入流中创建一个AudioStream对象
AudioStream as = new AudioStream(in);
AudioPlayer.player.start(as);//用静态成员player.start播放音乐
//AudioPlayer.player.stop(as);//关闭音乐播放
//如果要实现循环播放音乐剪辑java代码,则用下面的三句取代上面的“AudioPlayer.player.start(as);”这句
/*AudioData data = https://www.04ip.com/post/as.getData();
ContinuousAudioDataStream gg= new ContinuousAudioDataStream (data);
AudioPlayer.player.start(gg);// Play audio.
*/
//如果要用一个 URL 做为声音流的源(source),则用下面的代码所示替换输入流来创建声音流:
/*AudioStream as = new AudioStream (url.openStream());
*/
} catch(FileNotFoundException e){
System.out.print("FileNotFoundException ");
} catch(IOException e){
System.out.print("有错误!");
}
}
用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中添加背景音乐的代码不知道你是在java里哪添加?Swing界面中吗?
下面这个是我之前做Swing界面程序时添加音乐的代码,希望对你有帮助
AudioClip[] musics;//定义音乐集合
musics = new AudioClip[2];//初始化
URL url1 = this.getClass().getResource("/ReadyGo.WAV"); //定义音乐文件地址
URL url2 = this.getClass().getResource("/back1.mid"); //定义音乐文件地址
musics[0] = JApplet.newAudioClip(url1);
musics[1] = JApplet.newAudioClip(url2);
musics[0].play();//音乐开始执行
musics[1].stop();//停止播放
编写一个java类 , 实现mid或者wav格式的背景音乐播放,要循环的,用作一个小游戏的背景音乐,谢import java.applet.Applet;

推荐阅读