简体   繁体   中英

Getting an exception when adding audio to mediaplayer

So after I added the images that I wanted to add, I went and tried to add audio like this

 <MediaPlayer fx:id="gameIntro" autoPlay="true"volume="0.1">
  <media>
      <Media source="@AudioFiles/GameIntroTheme.MP3" />
  </media>
 </MediaPlayer>

but that didn't work.

Error that relates to the problem

Caused by: javafx.fxml.LoadException: 
file:/C:/Users/Owner/Documents/NetBeansProjects/MillionaireTriviaGame/dist/run30649974/MillionaireTriviaGame.jar!/millionairetriviagame/MenulayoutFXML.fxml:18

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2605)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2547)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at millionairetriviagame.MillionaireTriviaGame.start(MillionaireTriviaGame.java:17)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1270092040.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/355629945.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1753953479.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1915503092.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1963387170.run(Unknown Source)
... 1 more
Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[18,49]
Message: Element type "MediaPlayer" must be followed by either attribute specifications, ">" or "/>".
at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:601)
at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88)
at 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 who wants to be a millionaire");
    menuStage.setScene(menuScene);
    menuStage.show();
}

public static void main(String[] args) 
{
    launch(args);
}
}

A screenshot of my project directory

项目目录


EDIT

I tried loading a Media into the mediaplayer using the 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 and gives an NullpointerException and a javafx.fxml.loadException . But if I move the audio files folder outside of the src folder and the do this

 Media gameIntroTheme = new Media(new File("AudioFiles/GameIntroTheme.MP3").toURI().toString()); 

The program works. Please explain.

The best approach would be to add a MediaView to the fxml if you want to create a videoplayer, or do not add anything if you just want to play audio. Inside your controller, create instances of MediaPlayer and Media, load media and add them to MediaView, if required.

A FXML example can be found here . The corresponding controller can be found here .

so this is the answer

Media gameIntroTheme = new Media(getClass().getResource("/millionairetriviagame/AudioFiles/GameIntroTheme.mp3").toExternalForm()); 

Make sure that if you're are doing it in this case that the extension is the same as the file that you're working with.

credit to kiheru and itachi for helping me figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM