public class HelloJMF {
private JFrame frame=new JFrame("HelloJMF Player");
static Player player=null;
public HelloJMF(){
try {
//指定一个音频文件
URLurl=new URL("file:///E:/Java/JavaEE/workspaces/JMFlearn/src/main/resources/郑源&蒋姗倍-红尘情歌.mp3");
player=Manager.createRealizedPlayer(url);
}catch (MalformedURLException e) {
e.printStackTrace();
}catch (NoPlayerException e) {
e.printStackTrace();
}catch (CannotRealizeException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
//将播放控制条加到界面上
Componentcontrol=player.getControlPanelComponent();
frame.getContentPane().add(control,BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
@Override
public voidwindowClosing(WindowEvent e) {
super.windowClosing(e);
HelloJMF.stop();
System.exit(0);
}
});
frame.pack();
frame.setSize(newDimension(200, 50));
frame.setVisible(true);
player.start();
}
public staticvoid stop(){
player.stop();
player.close();
}
public staticvoid main(String[] args) {
new HelloJMF();
}
}
记得将JMF的jar包含进来。