使用mediaPlayer时程序无法启动

时间:2015-04-08 01:09:38

标签: java javafx media-player

问题:

一切正常,直到我决定将mediaPlayer的媒体添加到程序中。我知道mediaPlayer的罪魁祸首是因为我将运行mediaPlayer的函数设置为main函数的结尾,然后屏幕将显示第二个然后消失,因为如果我将函数设置为任何地方则根本不运行其他

导致问题的相关代码

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

2 个答案:

答案 0 :(得分:0)

Media constructor需要一个字符串,表示具有file:http:jar:方案的网址。

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

这样的东西
Media gameIntroTheme = 
    new Media(getClass().getResource("GameIntroTheme.MP3").toExternalForm());

答案 1 :(得分:0)

以下是答案

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