2015-04-08 42 views
0

问题:程序没有启动

一切工作正常,直到我决定与媒体播放器添加媒体到程序中。我知道mediaPlayer的原因是罪魁祸首是因为我将运行mediaPlayer的函数设置为主函数的结尾,然后屏幕出现秒,然后消失,反对根本不运行,如果我在任何地方设置函数其他。

导致该问题

private void playThemeIntro() 
{ 
    Media gameIntroTheme = new Media("GameIntroTheme.MP3"); 
    mediaPlayer = new MediaPlayer(gameIntroTheme); 
    mediaPlayer.setAutoPlay(true); 
} 

整个代码

package whowantstobeamillionairetriviagame; 

import java.io.File; 

import javafx.application.Application; 

import javafx.geometry.Insets; 
import javafx.geometry.Pos; 

import javafx.scene.Scene; 
import javafx.scene.media.Media; 
import javafx.scene.control.Button; 
import javafx.scene.image.Image; 
import javafx.scene.image.ImageView; 
import javafx.scene.layout.Background; 
import javafx.scene.layout.Priority; 
import javafx.scene.layout.StackPane; 
import javafx.scene.layout.VBox; 
import javafx.scene.media.MediaPlayer; 
import javafx.scene.paint.Paint; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.stage.Stage; 

public class WhoWantsToBeAMillionaireTriviaGame extends Application 
{ 
private VBox menuLayout; 
private MediaPlayer mediaPlayer; 

@Override 
public void start(Stage startingStage) throws Exception 
{ 
    StackPane backgroundSettings = new StackPane(); 

    Image backgroundColor = new Image("http://1.bp.blogspot.com/-p0s06MBIx_U/T8zKIBZ24pI/AAAAAAAAA7Y/n8hMZfpRic0/s1600/dark+blue+wallpaper+10.jpg"); 

    ImageView background = new ImageView(); 
    background.setImage(backgroundColor); 

    createMenuLayout(); 

    backgroundSettings.getChildren().addAll(background, menuLayout); 

    playThemeIntro(); // If you comment this out, the program works properly 

    Scene backgroundScene = new Scene(backgroundSettings); 
    startingStage.setScene(backgroundScene); 

    startingStage.setTitle("Who Wants to be a Millionaire"); 
    startingStage.show(); 
} 

private void playThemeIntro() 
{ 
    Media gameIntroTheme = new Media("GameIntroTheme.MP3"); 
    mediaPlayer = new MediaPlayer(gameIntroTheme); 
    mediaPlayer.setAutoPlay(true); 
} 

private VBox createMenuLayout() 
{ 
    menuLayout = new VBox(); 
    menuLayout.setSpacing(20); 
    menuLayout.setAlignment(Pos.TOP_CENTER); 

    Image millionaireLogo = new Image(new File("MillionaireLogo1.PNG").toURI().toString()); 

    ImageView logoPicture = new ImageView(); 
    logoPicture.setImage(millionaireLogo); 
    logoPicture.setPreserveRatio(true); 
    logoPicture.setSmooth(true); 
    logoPicture.setCache(true); 

    menuLayout.getChildren().add(logoPicture); 

    Button menuButtons[] = new Button[] 
    { 
     new Button("Play"), 
     new Button("Options"), 
     new Button("Help"), 
     new Button("Exit") 
    }; 

    for (int i = 0; i < 4; i++) 
    { 
     menuButtons[i].setPrefSize(200, 30); 
     Rectangle r = new Rectangle(200, 30, Paint.valueOf("346699")); 
     r.setArcHeight(30); 
     r.setArcWidth(30); 
     menuButtons[i].setOnMouseEntered(e -> r.setFill(Paint.valueOf("0f69b4"))); 
     menuButtons[i].setOnMouseExited(e -> r.setFill(Paint.valueOf("346699"))); 
     menuButtons[i].setBackground(Background.EMPTY); 
     menuButtons[i].setTextFill(Paint.valueOf("White")); 
     menuButtons[i].setFont(Font.font("Serif", FontWeight.BOLD, 16)); 
     VBox.setMargin(menuButtons[i], new Insets(0, 0, 0, 8)); 
     VBox.setVgrow(menuButtons[i], Priority.ALWAYS); 
     StackPane sp = new StackPane(); 
     sp.getChildren().addAll(r, menuButtons[i]); 
     menuLayout.getChildren().add(sp); 
    } 

    return menuLayout; 
} 

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

错误,我得到了

Exception in Application start method 
java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) 
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:497) 
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) 
Caused by: java.lang.RuntimeException: Exception in Application start method 
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182) 
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1642360923.run(Unknown Source) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'GameIntroTheme.MP3' 
at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:211) 
at javafx.scene.media.Media.<init>(Media.java:391) 
at whowantstobeamillionairetriviagame.WhoWantsToBeAMillionaireTriviaGame.playThemeIntro(WhoWantsToBeAMillionaireTriviaGame.java:57) 
at whowantstobeamillionairetriviagame.WhoWantsToBeAMillionaireTriviaGame.start(WhoWantsToBeAMillionaireTriviaGame.java:46) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863) 
at com.sun.javafx.application.LauncherImpl$$Lambda$53/792965399.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/266742917.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 
    Exception running application  whowantstobeamillionairetriviagame.WhoWantsToBeAMillionaireTriviaGame 
Java Result: 1 
+0

任何错误讯息? –

+0

不,它只是在我运行项目时开始和关闭。 –

+0

阅读[媒体文档](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/media/package-summary.html)以了解如何获取错误信息。 – jewelsea

回答

0

Media constructor相关法规要求,代表了file:一个URL字符串,http:jar:方案。

我不知道你的项目布局,所以我不能给出一个明确的答案,但你可能需要像

Media gameIntroTheme = 
    new Media(getClass().getResource("GameIntroTheme.MP3").toExternalForm()); 
+0

如果我的IDE(NetBeans)告诉我音频文件(GameIntroTheme.MP3)“似乎是二进制数据,那么它有多重要?您是否想在文本文件编辑器中打开它”当我在文件中单击它时然后继续说“文件'在这里插入文件的路径'不能用UTF-8编码安全地打开,你还想打开它吗?'”这意味着我的文件被损坏了任何方式?我还应该提到,我从一个拥有音频文件数据库的网站上下载了音频文件。 –

+0

您如何期望您的IDE打开音频文件?这只是二进制数据...不知道我理解那个评论。 –

+0

当我第一次点击音频文件时(我第一次见过这种类型的东西),我只是重新回到我的IDE对我说的话。可能是因为当你在文件目录中点击或者它不支持打开音频文件时,它没有任何方式打开音频文件。 –

0

下面是答案

Media gameIntroTheme = new Media(newFile("GameIntroTheme.MP3").toURI().toString()); 
+1

如果将应用程序捆绑为jar文件,那么这将不起作用,当然...... –

+0

好的,我很感兴趣,我怎样才能让它工作。 –

+0

你说得对。我在命令行中执行了jar cvf WhoWantsToBeAMillionaireTriviaGame.jar *,最后它捕获了filenotfoundexception。我将如何去解决这个问题? –

相关问题