java - Getting an exception when adding audio to mediaplayer -
so after added images wanted add, went , tried add audio this
<mediaplayer fx:id="gameintro" autoplay="true"volume="0.1"> <media> <media source="@audiofiles/gameintrotheme.mp3" /> </media> </mediaplayer>
but didn't work.
error relates problem
caused by: javafx.fxml.loadexception: file:/c:/users/owner/documents/netbeansprojects/millionairetriviagame/dist/run30649974/millionairetriviagame.jar!/millionairetriviagame/menulayoutfxml.fxml:18 @ javafx.fxml.fxmlloader.constructloadexception(fxmlloader.java:2605) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2547) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2445) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3218) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3179) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3152) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3128) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:3108) @ javafx.fxml.fxmlloader.load(fxmlloader.java:3101) @ millionairetriviagame.millionairetriviagame.start(millionairetriviagame.java:17) @ com.sun.javafx.application.launcherimpl.lambda$launchapplication1$159(launcherimpl.java:863) @ com.sun.javafx.application.launcherimpl$$lambda$53/1270092040.run(unknown source) @ com.sun.javafx.application.platformimpl.lambda$runandwait$172(platformimpl.java:326) @ com.sun.javafx.application.platformimpl$$lambda$46/355629945.run(unknown source) @ com.sun.javafx.application.platformimpl.lambda$null$170(platformimpl.java:295) @ com.sun.javafx.application.platformimpl$$lambda$48/1753953479.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ com.sun.javafx.application.platformimpl.lambda$runlater$171(platformimpl.java:294) @ com.sun.javafx.application.platformimpl$$lambda$47/1915503092.run(unknown source) @ com.sun.glass.ui.invokelaterdispatcher$future.run(invokelaterdispatcher.java:95) @ com.sun.glass.ui.win.winapplication._runloop(native method) @ com.sun.glass.ui.win.winapplication.lambda$null$145(winapplication.java:101) @ com.sun.glass.ui.win.winapplication$$lambda$36/1963387170.run(unknown source) ... 1 more caused by: javax.xml.stream.xmlstreamexception: parseerror @ [row,col]:[18,49] message: element type "mediaplayer" must followed either attribute specifications, ">" or "/>". @ com.sun.org.apache.xerces.internal.impl.xmlstreamreaderimpl.next(xmlstreamreaderimpl.java:601) @ javax.xml.stream.util.streamreaderdelegate.next(streamreaderdelegate.java:88) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2517) ... 22 more exception running application millionairetriviagame.millionairetriviagame java result: 1
my fxml file
<?xml version="1.0" encoding="utf-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.image.*?> <?import javafx.scene.media.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <stackpane fx:id="mainmenu" xmlns:fx="http://javafx.com/fxml/1" fx:controller="millionairetriviagame.menulayoutfxmlcontroller"> <imageview> <image> <image url="@imagefiles/bluebackgroundcolor.jpg"/> </image> </imageview> <mediaplayer fx:id="gameintro" autoplay="true"volume="0.1"> <media> <media source="@audiofiles/gameintrotheme.mp3" /> </media> </mediaplayer> <vbox fx:id="mainmenulayout" spacing="20" alignment="top_center" > <imageview> <image> <image url="@imagefiles/millionairelogo1.png"/> </image> </imageview> </vbox> </stackpane>
my main class
package millionairetriviagame; import javafx.application.application; import javafx.fxml.fxmlloader; import javafx.scene.parent; import javafx.scene.scene; import javafx.stage.stage; public class millionairetriviagame extends application { @override public void start(stage menustage) throws exception { parent object = fxmlloader.load(getclass().getresource("menulayoutfxml.fxml")); scene menuscene = new scene(object); menustage.settitle("let's play wants millionaire"); menustage.setscene(menuscene); menustage.show(); } public static void main(string[] args) { launch(args); } }
a screenshot of project directory
edit
i tried loading media mediaplayer using following code
@override public void initialize(url url, resourcebundle rb) { media gameintrotheme = new media(getclass().getresource("audiofiles/gameintrotheme.mp3").toexternalform()); mediaplayer mediaplayer = new mediaplayer(gameintrotheme); mediaplayer.setautoplay(true); mediaplayer.setvolume(0.1); }
it doesn't work , gives nullpointerexception
, javafx.fxml.loadexception
. if move audio files folder outside of src
folder ,
media gameintrotheme = new media(new file("audiofiles/gameintrotheme.mp3").touri().tostring());
the program works. please explain.
so answer
media gameintrotheme = new media(getclass().getresource("/millionairetriviagame/audiofiles/gameintrotheme.mp3").toexternalform());
make sure if you're doing in case extension same file you're working with.
credit kiheru , itachi helping me figure out.