用java编写MP3播放器

考试作业不要求多么完善只要能连接歌词就行... 考试作业 不要求多么完善 只要能连接歌词就行 展开
 我来答
匿名用户
2013-11-26
展开全部
作业其实还是自己写的好。要用到JMF包啊,到网上下载一个JMF包,照着说明安装上。
以下是我写的一个很简单的播放器,只能播放mp3,mpeg,mpg,wav等简单的格式。
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.media.ControllerEvent;
import javax.media.ControllerListener;
import javax.media.MediaLocator;
import javax.media.RealizeCompleteEvent;
import javax.media.bean.playerbean.MediaPlayer;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JList;
import java.awt.BorderLayout;
import javax.swing.JSplitPane;
import java.awt.Component;
import java.util.Vector;

public class JMF_T extends JFrame implements ControllerListener, ActionListener {
MediaPlayer Player;

private String filename = "";

private static final long serialVersionUID = 1L;

private Vector vct = new Vector(); // @jve:decl-index=0:

private JPanel jContentPane = null;

private JSplitPane jSplitPane = null;

private JPanel playPanel = null;

private JList jList = null;

private JSplitPane getJSplitPane() {
if (jSplitPane == null) {
jSplitPane = new JSplitPane();
jSplitPane.setDividerSize(5);
jSplitPane.setResizeWeight(0.8);
jSplitPane.setRightComponent(getJList());
jSplitPane.setLeftComponent(getPlayPanel());
}
return jSplitPane;
}

private JPanel getPlayPanel() {
if (playPanel == null) {
playPanel = new JPanel();
playPanel.setLayout(new BorderLayout());
}
return playPanel;
}

private JList getJList() {
if (jList == null) {
jList = new JList();
jList.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent e) {
// TODO Auto-generated Event stub mouseClicked()
if (e.getClickCount() == 1) {
String str = (String) jList.getSelectedValue();
if (str == null) {
return;
}
filename = str;
System.out.println(str);
}
if (e.getClickCount() == 2) {
String str = (String) jList.getSelectedValue();
if (str == null) {
return;
}
filename = str;
play();
}
}
});
}
return jList;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JMF_T thisClass = new JMF_T();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}

public JMF_T() {
initialize();
}

private void OpenFile() {
FileDialog fd = new FileDialog(this, "Choose Video", FileDialog.LOAD);
fd.setVisible(true);
filename = fd.getDirectory() + fd.getFile();
System.out.println(filename);
if (filename.equals("")) {
return;
} else if (filename.equals("nullnull")) {
return;
}
boolean j = false;
for (int i = 0; i < vct.size(); i++) {
if (vct.get(i).toString().equals(filename)) {
j = true;
break;
}
}
if (j == false) {
vct.add(filename);
jList.setListData(vct);
}
}

private void stop() {
if (Player != null) {
Player.stop();
Player.deallocate();
}
}

private void play() {
try {
if (filename.equals("")) {
return;
}
if (Player == null) {
Player = new MediaPlayer();
} else {
closePreviosPlayer();
}
Player.setMediaLocator(new MediaLocator("file:///" + filename));
Player.addControllerListener(this);
Player.realize();
Player.start();
} catch (Exception e) {

}
}

public void actionPerformed(ActionEvent e) {
String action = e.getActionCommand().toString();
if (action.equals("Open")) {
OpenFile();
}
if (action.equals("Play")) {
play();
}
if (action.equals("Stop")) {
stop();
}
if (action.equals("Exit")) {
dispose();
System.exit(0);
}
}

private void initialize() {
this.setSize(500, 350);
setLocation(300, 100);
this.setContentPane(getJContentPane());
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}
});
MenuBar mb = new MenuBar();
setMenuBar(mb);
Menu fileMenu = new Menu("File");
Menu actMenu = new Menu("Action");
mb.add(fileMenu);
mb.add(actMenu);
MenuItem itemOpen = new MenuItem("Open");
itemOpen.addActionListener(this);
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();
}

}
}
匿名用户
2013-11-26
展开全部
既然是作业,就不能帮你做了,还是自己做的好,如果你有什么难点,可以去下一个类似的mp3程序,看看别人的思路,在比较权威的技术网站上都有,当然,如果你想下一个现成的去交作业,也可以,想学知识还是为了应付老师,全部取决于你自己~
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-11-26
展开全部
你要不 我可以发给你 要单独叫我
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式