向媒体播放器添加音频时获取异常

时间:2015-04-10 00:20:51

标签: java audio javafx fxml

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

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

但这没效果。

与问题相关的错误

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

我的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>

我的主要课程

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);
}
}

我的项目目录的截图

Project Directory


修改

我尝试使用以下代码将媒体加载到媒体播放器

@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); 
}

它不起作用,并提供NullpointerExceptionjavafx.fxml.loadException。但是,如果我将音频文件文件夹移到src文件夹之外并执行此操作

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

该计划有效。请解释一下。

2 个答案:

答案 0 :(得分:0)

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

FXML示例可以是found here。相应的控制器可以是found here

答案 1 :(得分:0)

所以这就是答案

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

如果你在这种情况下这样做,请确保扩展名与您正在使用的文件相同。

归功于kiheru和itachi帮助我解决问题。