简体   繁体   English

向Mediaplayer添加音频时出现异常

[英]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 我的FXML文件

<?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 . 它不起作用,并提供了NullpointerExceptionjavafx.fxml.loadException But if I move the audio files folder outside of the src folder and the do this 但是如果我将音频文件文件夹移到src文件夹之外,请执行此操作

 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. 最好的方法是,如果您要创建MediaView ,则将MediaView添加到fxml中,或者如果您只想播放音频,则不添加任何内容。 Inside your controller, create instances of MediaPlayer and Media, load media and add them to MediaView, if required. 在控制器内部,创建MediaPlayer和Media实例,加载媒体并将它们添加到MediaView(如果需要)。

A FXML example can be found here . 可以在这里找到 FXML示例。 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. 感谢kiheru和itachi帮助我解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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