2015-04-08 41 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。我將如何去解決這個問題? –

相關問題