我正在开发我认为应该是一个简单的任务,javafx-媒体播放器将播放存储在本地硬盘上的mp3文件,用户默认音乐文件夹路径(使用fedora)。我可以从命令行播放所有mp3文件,但是当我尝试通过javafx.scene.media库播放时,javafx一直告诉我mp3是一种支持的媒体类型。我已经下载了我能找到的每个图书馆,但无济于事。以下是实际尝试打开文件并播放媒体的代码:
首次尝试:
public void playTrack( String audioFileName ) {
System.out.println("going to play the file "+audioFileName);
String filename = System.getProperty("user.home")+"/"+audioFileName;
File audioFile = new File( filename);
Media audioFileStream= null;
try {
audioFileStream = new Media(audioFile.toURI().toURL().toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MediaPlayer mediaPlayer = new MediaPlayer(audioFileStream);
mediaPlayer.play();
}
方法2:
public void playTrack( String audioFileName ) {
System.out.println("going to play the file "+audioFileName);
String source = new File(System.getProperty("user.home")+"/"+audioFileName).toURI().toString();
//String source = new File("07-Basket-Case.mp3").toURI().toString();
System.out.println("going to play "+source);
Media media = null;
media = new Media(source);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
}
两者都产生完全相同的错误结果,如下所示:
要播放文件:/home/cpeddie/workspace/parnassusfx/07-Basket-Case.mp3 线程中的异常" JavaFX应用程序线程" MediaException:MEDIA_UNSUPPORTED:com.sun.media.jfxmedia.MediaException:"错误的媒体音频格式不受支持" :com.sun.media.jfxmedia.MediaException:"错误的媒体音频格式不受支持" at javafx.scene.media.MediaException.exceptionToMediaException(MediaException.java:125) 在javafx.scene.media.MediaPlayer.init(MediaPlayer.java:474) 在javafx.scene.media.MediaPlayer。(MediaPlayer.java:383) 在com.promptu.parnassusfx.sample.music.MusicController.playTrack(MusicController.java:218) 在com.promptu.parnassusfx.sample.music.MusicController $ 3 $ 1 $ 1.handle(MusicController.java:357) 在com.promptu.parnassusfx.sample.music.MusicController $ 3 $ 1 $ 1.handle(MusicController.java:1) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28) 在javafx.event.Event.fireEvent(Event.java:171) 在javafx.scene.Node.fireEvent(Node.java:6867) 在javafx.scene.control.Button.fire(Button.java:179) at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193) 在com.sun.javafx.scene.control.skin.SkinBase $ 4.handle(SkinBase.java:336) 在com.sun.javafx.scene.control.skin.SkinBase $ 4.handle(SkinBase.java:329) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33) 在javafx.event.Event.fireEvent(Event.java:171) 在javafx.scene.Scene $ MouseHandler.process(Scene.java:3311) 在javafx.scene.Scene $ MouseHandler.process(Scene.java:3151) 在javafx.scene.Scene $ MouseHandler.access $ 1900(Scene.java:3106) 在javafx.scene.Scene.impl_processMouseEvent(Scene.java:1563) 在javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2248) at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:250) at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:173) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:292) 在com.sun.glass.ui.View.handleMouseEvent(View.java:530) 在com.sun.glass.ui.View.notifyMouse(View.java:924) at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method) 在com.sun.glass.ui.gtk.GtkApplication $ 3 $ 1.run(GtkApplication.java:89) 在java.lang.Thread.run(Thread.java:724) 引起:com.sun.media.jfxmedia.MediaException:"错误的媒体音频格式不受支持" at com.sun.media.jfxmediaimpl.MediaUtils.nativeError(MediaUtils.java:331) 在com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMedia.init(GSTMedia.java:50) 在com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMedia。(GSTMedia.java:33) 在com.sun.media.jfxmediaimpl.platform.gstreamer.GSTMediaPlayer。(GSTMediaPlayer.java:39) 在com.sun.media.jfxmediaimpl.platform.gstreamer.GSTPlatform.prerollMediaPlayer(GSTPlatform.java:100) 在com.sun.media.jfxmediaimpl.platform.PlatformManager.createMediaPlayer(PlatformManager.java:173) 在com.sun.media.jfxmediaimpl.NativeMediaManager.getPlayer(NativeMediaManager.java:260) 在com.sun.media.jfxmedia.MediaManager.getPlayer(MediaManager.java:139) 在javafx.scene.media.MediaPlayer.init(MediaPlayer.java:431) ......还有75个
我还应该提一下,我正在使用运行在OS 10.8.5的MacBook Pro上的VMWare融合来开发它。运行Fedora 19。
任何帮助都会被证实!
答案 0 :(得分:3)
花了大约一天半来搞清楚,但需要安装以下软件包:
狄拉克库-1.0.2-9.fc18.i686.rpm
libavcodec52-0.7.15-32.fc18.i686.rpm
libavformat52-0.7.15-32.fc18.i686.rpm
libavutil50-0.7.15-32.fc18.i686.rpm
libxavs1-0.1.51-2.fc18.i686.rpm
安装好后,javafx就像魅力一样播放音乐。