如何使用JavaFX播放声音 [英] How to play sounds with JavaFX

查看:206
本文介绍了如何使用JavaFX播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用JavaFX.我知道它的基本原理.我尝试使用媒体和mediaplayer类播放声音,称为"sound.mp3".我正在eclipse中编程,并且声音文件在src文件夹中,与(默认程序包)"相同.这是我的代码:

I just started working with JavaFX. I know how the basics of it work. I tried to use the media and the mediaplayer classes to play a sound, called "sound.mp3". I am programming in eclipse, and I have the sound file in the src folder, the same folder as "(default package)". Here is my code:

import javafx.scene.media.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Hello World!");

        String ssound = "sound.mp3";
        Media sound = new Media(ssound);
        MediaPlayer mediaPlayer = new MediaPlayer(sound);
        mediaPlayer.play();

        StackPane root = new StackPane();
        primaryStage.setScene(new Scene(root, 800, 450));
        primaryStage.show();
    }
}

请告诉我我在做错什么.

Please tell me what I'm doing wrong.

这是控制台中的错误消息:

Here is the error message thing from the console:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
    at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:724) Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null!
    at com.sun.media.jfxmedia.locator.Locator.<init>(Unknown Source)
    at javafx.scene.media.Media.<init>(Unknown Source)
    at Main.start(Main.java:16)
    at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$5.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl$4$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
    at com.sun.glass.ui.win.WinApplication$3$1.run(Unknown Source) ... 1 more

推荐答案

只是其他答案所说的工作汇编:

Just a working compilation of what the other answers say:

String musicFile = "StayTheNight.mp3";     // For example

Media sound = new Media(new File(musicFile).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();

将音乐文件以及 bin src 一起添加到Project文件夹中.

Add the music file in your Project folder, alongside bin and src.

任何IDE都会提示您也添加它们:

Any IDE will prompt you to add these as well:

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;

像魅力一样工作.

这篇关于如何使用JavaFX播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆