如何使用 JavaFX 播放声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23202272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:18:35  来源:igfitidea点击:

How to play sounds with JavaFX

javaaudiojavafxmedia

提问by user3010445

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:

我刚开始使用 JavaFX。我知道它的基本原理是如何工作的。我尝试使用媒体和媒体播放器类来播放一种名为“sound.mp3”的声音。我在eclipse中编程,我在src文件夹中有声音文件,与“(默认包)”相同的文件夹。这是我的代码:

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
Media hit = new Media(new File(soundFilename).toURI().toString());
0(Unknown Source) at com.sun.javafx.application.LauncherImpl.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.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.run(Unknown Source) at com.sun.javafx.application.PlatformImpl.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.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.access0(Unknown Source) at com.sun.glass.ui.win.WinApplication.run(Unknown Source) ... 1 more

回答by KisnardOnline

This is what I am currently using:

这是我目前正在使用的:

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

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

回答by anaik

Just a working compilation of what the other answers say:

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

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

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

将音乐文件添加到您的项目文件夹中,旁边是binsrc

Any IDE will prompt you to add these as well:

任何 IDE 都会提示您添加这些:

mApplause = new AudioClip(this.getClass().getResource("/sounds/applause.mp3").toExternalForm());

Works like a charm.

奇迹般有效。

回答by Enes

new Media("file:sound.wav")

So this is what i used and it worked, i know it doesn't probably matter anymore but since it got in my google search while i was looking for something else i thought i would answer to it. :)

所以这就是我使用的并且有效,我知道它可能不再重要,但是因为它在我寻找其他东西时进入了我的谷歌搜索,我想我会回答它。:)

Meaning the toExternal Form it tells to form a url form of the path file.

这意味着 toExternal Form 它告诉形成路径文件的 url 形式。

回答by Bonfra

it depends on where is the audio file. (also it accepts only .wav) if the clip is outside the src folder you need to use

这取决于音频文件在哪里。(它也只接受 .wav)如果剪辑在您需要使用的 src 文件夹之外

##代码##

if it's not it's ok how are you doing

如果不是,没关系,你好吗