JMF现在还不支持WMA,如果要播放WMA文件,必须通过JNI来调本地的控件来实现了,在这里我给出来的例子是播放MP3的.
首先到SUN的网站上去下载JMF的安装程序,安装好后,我们只要在其安装目录下的lib文件夹中的jmf.jar和sound.jar加入到我们工程的classpath中就可以了.
接下来说是编码实现了:
private void playMusic(){
try {
javax.media.Player player;
if(null==player){
File musicFile=new File("./music.mp3");//得到一个MP3文件
if(musicFile.exists()){
MediaLocator locator=new MediaLocator("file:"+musicFile.getAbsolutePath());
player = Manager.createRealizedPlayer(locator);
player.prefetch();//预读文件
}else{
System.err.println(pro.getMusicFile()+" 找不到");
}
}
//centerpanel.add(player.getControlPanelComponent()!=null?player.getControlPanelComponent():null);//显示播放工具
player.start();//播放
} catch (CannotRealizeException ex) {
ex.printStackTrace();
} catch (NoPlayerException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
JMF对MP3支持的简单实现
最新推荐文章于 2020-07-31 01:21:32 发布