我写了一个简单的代码,想选择好音乐文件后就能播放音乐,请问该怎么做,(我已经装了JMF)
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class MyDemo extends JFrame implements ActionListener{
public MyDemo(){
JMenuBar JMB=new JMenuBar();
JMenu m1=new JMenu("File");
JMB.add(m1);
JMenuItem mi1=new JMenuItem("打开");
mi1.addActionListener(this);
m1.add(mi1);
this.setJMenuBar(JMB);
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
JFileChooserDemo j=new JFileChooserDemo(this);
}
}
public static void main(String[]args){
MyDemo aa = new MyDemo();
}
}class JFileChooserDemo extends JFileChooser{
JFileChooserDemo(MyDemo demo){
JFileChooser c=new JFileChooser();
c.setDialogTitle("打开文件");
c.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=c.showOpenDialog(this);
}
}
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class MyDemo extends JFrame implements ActionListener{
public MyDemo(){
JMenuBar JMB=new JMenuBar();
JMenu m1=new JMenu("File");
JMB.add(m1);
JMenuItem mi1=new JMenuItem("打开");
mi1.addActionListener(this);
m1.add(mi1);
this.setJMenuBar(JMB);
this.setSize(400,400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("打开"))
{
JFileChooserDemo j=new JFileChooserDemo(this);
}
}
public static void main(String[]args){
MyDemo aa = new MyDemo();
}
}class JFileChooserDemo extends JFileChooser{
JFileChooserDemo(MyDemo demo){
JFileChooser c=new JFileChooser();
c.setDialogTitle("打开文件");
c.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result=c.showOpenDialog(this);
}
}
只需要两句:
try {
MediaPlayer m = new MediaPlayer();
m.setDataSource(Manager.createDataSource(new MediaLocator("file://E:/TOMMY/workspace/huawei_remotesynthesize_run/audio/01.悔过诗.mp3")));
m.start();
} catch (NoDataSourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
"执行方式:java SimpleAudioPlayer 你的mp3文件路径"
不过我不知道怎么停止,如果你知道了,高诉我一下哦。import javax.media.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
public class SimpleAudioPlayer {
private Player audioPlayer = null;
public SimpleAudioPlayer(URL url) throws IOException,NoPlayerException,CannotRealizeException
{
audioPlayer = Manager.createRealizedPlayer(url);
}
public SimpleAudioPlayer(File file) throws IOException,NoPlayerException,CannotRealizeException
{
this(file.toURL());
}public void play()
{
audioPlayer.start();
}
public void stop()
{
audioPlayer.stop();
audioPlayer.close();
}
public static void main(String args[]) throws Exception
{
File audioFile = new File(args[0]);
SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile);
player.play();
//player.stop();
}
}