Java 向mediaplayer添加音频时出现异常

Java 向mediaplayer添加音频时出现异常,java,audio,javafx,fxml,Java,Audio,Javafx,Fxml,所以在我添加了我想添加的图像之后,我尝试添加这样的音频 <MediaPlayer fx:id="gameIntro" autoPlay="true"volume="0.1"> <media> <Media source="@AudioFiles/GameIntroTheme.MP3" /> </media> </MediaPlayer> 我的FXML文件 <?xml version="1.0" encod

所以在我添加了我想添加的图像之后,我尝试添加这样的音频

 <MediaPlayer fx:id="gameIntro" autoPlay="true"volume="0.1">
  <media>
      <Media source="@AudioFiles/GameIntroTheme.MP3" />
  </media>
 </MediaPlayer>
我的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>
我的项目目录的屏幕截图

编辑 我尝试使用以下代码将媒体加载到mediaplayer中

@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); 
}
它不工作,并给出一个NullpointerException和一个javafx.fxml.loadException。但是如果我将音频文件文件夹移到src文件夹之外,并且

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

这个程序有效。请解释。

如果您想创建视频播放器,最好的方法是在fxml中添加一个,如果您只想播放音频,则不要添加任何内容。在控制器内部,创建MediaPlayer和媒体的实例,加载媒体并将其添加到MediaView(如果需要)

可以使用FXML示例。相应的控制器可以是。

所以这就是答案

Media gameIntroTheme = new Media(getClass().getResource("/millionairetriviagame/AudioFiles/GameIntroTheme.mp3").toExternalForm()); 
如果在这种情况下执行此操作,请确保扩展名与正在使用的文件相同


感谢kiheru和itachi帮我解决了这个问题。

我想最好是告诉你我想如何实现这些音频文件,然后从那里开始。我计划稍后添加其他音频文件,特别是这个audiogameIntroTheme.mp3文件将仅在后台播放。实际上,它们都将在后台播放,因此,除非用户转到另一个屏幕,否则我在整个过程中不需要mediaview。在这种情况下,此后将不再播放。然后,我计划添加另一个音频文件,在用户返回主菜单或退出程序之前一直播放。您不需要MediaView。在媒体中加载音频文件,初始化媒体播放器并为其分配媒体,然后播放播放器。当你转到另一个屏幕时,停止mediaplayer->将其他音频加载到媒体->将其设置为mediaplayer并播放。好的,这是我的尝试@重写public void initializeURL url,ResourceBundle rb{Media gameIntroTheme=new MediagetClass.getResourceAudioFiles/gameIntroTheme.MP3.toExternalForm;MediaPlayer MediaPlayer=new MediaPlayergameIntroTheme;MediaPlayer.setAutoPlaytrue;MediaPlayer.setVolume0.1;}它不起作用主要是因为它得到了一个nullpointerException和一个javafx.fxml.LoadException文件的路径看起来大约正确,它和图像文件在同一个位置,所以我不知道出了什么问题。如果我将音频文件文件夹移到src文件夹之外,然后使用Dothismedia gameIntroTheme=new Media newFileAudioFiles/GameIntroTheme.MP3.toURI.toString;这个程序可以运行,但是为什么其他方法不能运行呢。请解释一下itachi
 Media gameIntroTheme = new Media(new File("AudioFiles/GameIntroTheme.MP3").toURI().toString()); 
Media gameIntroTheme = new Media(getClass().getResource("/millionairetriviagame/AudioFiles/GameIntroTheme.mp3").toExternalForm());