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()
}
}
播放音乐的代码 了解一下 可以直接加到小游戏里
package com.music.testimport javazoom.jl.decoder.JavaLayerException
import javazoom.jl.player.Player
import java.io.*
public class Music {
private String music
private Player player
public Music() throws FileNotFoundException, JavaLayerException {
FileInputStream file = new FileInputStream("e:/闹钟.mp3")
BufferedInputStream name = new BufferedInputStream(file)
player = new Player(name)
player.play()
}
public static void main(String[] args) throws FileNotFoundException, JavaLayerException{
new Music()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)