Java编程实现音乐播放
我想写一个javaApplication图形界面的程序。应该怎样实现音乐的播放呢?无论哪种格式的音乐只要能播放一种格式就成。求高手指点。...
我想写一个java Application图形界面的程序。应该怎样实现音乐的播放呢?无论哪种格式的音乐只要能播放一种格式就成。求高手指点。
展开
3个回答
展开全部
这个程序只要写对了音乐文件的URL地址,例如:new URL("file:/C:/tmp/1/Windows Ding.wav");
就可以播放音乐,除了可以播放.wav格式的音乐,还可以播放.au格式的音乐。
另外,如果你不希望音乐循环播放,你可以去掉audio1.loop();这一语句。
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
public class D extends JFrame{
D(){
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
URL codebase=null;
try {
codebase = new URL("file:/C:/tmp/1/Windows Ding.wav");
} catch (MalformedURLException e) {
e.printStackTrace();
}
AudioClip audio1=Applet.newAudioClip(codebase);
audio1.loop();
}
public static void main(String[] args) {
new D();
}
}
就可以播放音乐,除了可以播放.wav格式的音乐,还可以播放.au格式的音乐。
另外,如果你不希望音乐循环播放,你可以去掉audio1.loop();这一语句。
import java.applet.AudioClip;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;
public class D extends JFrame{
D(){
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
URL codebase=null;
try {
codebase = new URL("file:/C:/tmp/1/Windows Ding.wav");
} catch (MalformedURLException e) {
e.printStackTrace();
}
AudioClip audio1=Applet.newAudioClip(codebase);
audio1.loop();
}
public static void main(String[] args) {
new D();
}
}
展开全部
这个需要jmf的相关包,去网上下载下,给你写了个例子
public class TestPlay extends JFrame {
private Component vc, cc;
private JButton file = new JButton("file");
private Player player = null;
public TestPlay() throws HeadlessException, NoPlayerException, MalformedURLException, IOException {
this.add(file);
file.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser f = new JFileChooser();
if(f.showOpenDialog(null)==JFileChooser.CANCEL_OPTION)return;
try {
player = Manager.createPlayer(f.getSelectedFile().toURL());
player.addControllerListener(new ControllerListener() {
public void controllerUpdate(ControllerEvent arg0) {
controllerUpdateImp(arg0);
}
});
player.prefetch();
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
});
}
public void controllerUpdateImp(ControllerEvent e) {
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
player.start();
} else if (e instanceof PrefetchCompleteEvent) {
player.start();
} else if (e instanceof RealizeCompleteEvent) {
vc = player.getVisualComponent();
if (vc != null) this.add(vc, BorderLayout.CENTER);
cc = player.getControlPanelComponent();
if (cc != null) this.add(cc, BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
this.setVisible(true);
} else if (e instanceof ControllerClosedEvent) {
System.exit(0);
}
}
public static void main(String[] args) {
TestPlay t;
try {
t = new TestPlay();
t.setDefaultCloseOperation(t.EXIT_ON_CLOSE);
t.pack();
t.setLocationRelativeTo(null);
t.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
public class TestPlay extends JFrame {
private Component vc, cc;
private JButton file = new JButton("file");
private Player player = null;
public TestPlay() throws HeadlessException, NoPlayerException, MalformedURLException, IOException {
this.add(file);
file.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JFileChooser f = new JFileChooser();
if(f.showOpenDialog(null)==JFileChooser.CANCEL_OPTION)return;
try {
player = Manager.createPlayer(f.getSelectedFile().toURL());
player.addControllerListener(new ControllerListener() {
public void controllerUpdate(ControllerEvent arg0) {
controllerUpdateImp(arg0);
}
});
player.prefetch();
} catch (Exception e) {
System.out.println(e);
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
});
}
public void controllerUpdateImp(ControllerEvent e) {
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0));
player.start();
} else if (e instanceof PrefetchCompleteEvent) {
player.start();
} else if (e instanceof RealizeCompleteEvent) {
vc = player.getVisualComponent();
if (vc != null) this.add(vc, BorderLayout.CENTER);
cc = player.getControlPanelComponent();
if (cc != null) this.add(cc, BorderLayout.SOUTH);
this.pack();
this.setResizable(false);
this.setVisible(true);
} else if (e instanceof ControllerClosedEvent) {
System.exit(0);
}
}
public static void main(String[] args) {
TestPlay t;
try {
t = new TestPlay();
t.setDefaultCloseOperation(t.EXIT_ON_CLOSE);
t.pack();
t.setLocationRelativeTo(null);
t.setVisible(true);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
InputStream is =null;
AudioStream as = null ;
is = getClass().getResourceAsStream("4.wav");
try {
as = new AudioStream(is);
} catch (IOException e) {}
AudioPlayer.player.start(as);
//AudioPlayer.player.stop(as);
AudioStream as = null ;
is = getClass().getResourceAsStream("4.wav");
try {
as = new AudioStream(is);
} catch (IOException e) {}
AudioPlayer.player.start(as);
//AudioPlayer.player.stop(as);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |