关于用java播放mp3代码的信息( 四 )


Menu fileMenu = new Menu("File");
Menu actMenu = new Menu("Action");
mb.add(fileMenu);
mb.add(actMenu);
MenuItem itemOpen = new MenuItem("Open");
itemOpen.addActionListener(this);
【关于用java播放mp3代码的信息】fileMenu.add(itemOpen);
fileMenu.addSeparator();
MenuItem itemPlay = new MenuItem("Play");
itemPlay.addActionListener(this);
actMenu.add(itemPlay);
actMenu.addSeparator();
MenuItem itemStop = new MenuItem("Stop");
itemStop.addActionListener(this);
actMenu.add(itemStop);
MenuItem itemExit = new MenuItem("Exit");
itemExit.addActionListener(this);
fileMenu.add(itemExit);
this.validate();
this.setVisible(true);
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
jContentPane.add(getJSplitPane(), BorderLayout.CENTER);
}
return jContentPane;
}
private void closePreviosPlayer() {
if (Player == null)
return;
Player.stop();
Player.deallocate(); // 停止播放并且重新装载DateSource
Component visual = Player.getVisualComponent();
Component control = Player.getControlPanelComponent();
if (visual != null) {
playPanel.remove(visual);
}
if (control != null) {
playPanel.remove(control);
}
}
public synchronized void controllerUpdate(ControllerEvent event) {
if (event instanceof RealizeCompleteEvent) {
Component comp;
if ((comp = Player.getControlPanelComponent()) != null) {
playPanel.add("South", comp);
} else {
closePreviosPlayer();
}
if ((comp = Player.getVisualComponent()) != null) {
playPanel.add("Center", comp);
}
validate();
}
}
}
java如何读取MP3格式文件的内容然后对其播放实例代码直接下个JMF,google搜,sun上有~~然后安装目录是你的JDK,
再播放MP3文件就:
import javax.media.*;
import java.net.MalformedURLException;
import java.net.URL;
Player player;
File playFile=new File("");//你的MP3文件
try {
player=Manager.createRealizedPlayer(playFile.toURL());
player.prefetch();
player.setMediaTime(new Time(10.0));
player.start();
} catch (NoPlayerException e1)
{
e1.printStackTrace();
} catch (CannotRealizeException e1)
{
e1.printStackTrace();
} catch (MalformedURLException e1)
{
e1.printStackTrace();
} catch (IOException e1)
{
e1.printStackTrace();
}
这种基本代码,自己搜一下,用编译器熟悉熟悉JMF的方法就会了 。。。我以前回答别人时的答案~~~
java编写 mp3播放器 代码// 你看看吧 。。必须下载 jmf包 如果不知道下载就问我吧
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.media.bean.playerbean.MediaPlayer; //必须下载 jmf 媒体播放包
public class player extends Applet implements ActionListener {
Button b1, b2;
MediaPlayer player;
public void init() {
player = new MediaPlayer();
setLayout(new FlowLayout());
try{
player.setMediaLocation("file:/F:\\音乐\\mp3\\黑白配.mp3");// file:/不能删除 音频文件路径
} catch (Exception e) {
System.out.println("文件不存在");
}
b1 = new Button("播放");
b2 = new Button("停止");
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(200, 200);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
player.start();
} else if (e.getSource() == b2) {
player.stop();
System.out.println(player.getMediaTime().getSeconds());

推荐阅读