简体   繁体   English

如何使用 JavaFX 播放声音

[英]How to play sounds with JavaFX

I just started working with JavaFX.我刚开始使用 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".我尝试使用媒体和媒体播放器类来播放一种名为“sound.mp3”的声音。 I am programming in eclipse, and I have the sound file in the src folder, the same folder as "(default package)".我在eclipse中编程,我在src文件夹中有声音文件,与“(默认包)”相同的文件夹。 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();

Add the music file in your Project folder, alongside bin and src .将音乐文件添加到您的项目文件夹中,并与binsrc一起。

Any IDE will prompt you to add these as well:任何 IDE 都会提示您添加这些:

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

Works like a charm.奇迹般有效。

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

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 形式。

这是我目前正在使用的:

Media hit = new Media(new File(soundFilename).toURI().toString());

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 文件夹之外

new Media("file:sound.wav")

if it's not it's ok how are you doing如果不是,没关系,你好吗

The other answers here didn't work for me, but this did the job:这里的其他答案对我不起作用,但这完成了工作:

Media sound = new Media(getClass().getResource("sounds/sound.mp3").toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();

I had the same problem.我有同样的问题。 In my case, it was about accesses given to javaFX.就我而言,它是关于对 javaFX 的访问。 Assuming that you use IntelliJ IDEA, go to Run -> Edit Configurations, select "Modify Options", click on "Add VM Options", and paste this: --module-path /PATH/TO/YOUR/JAVAFX/LIB --add-modules javafx.controls,javafx.fxml,javafx.base,javafx.media (Replace /PATH/TO/YOUR/JAVAFX/LIB with directory of the "lib" folder in javafx installation directory on your computer. This is similar to what is said in this official tutorial: Create-a-javafx-project-with-IntelliJ ; But modules javafx.media and javafx.base are added.假设您使用 IntelliJ IDEA,转到 Run -> Edit Configurations,选择“Modify Options”,单击“Add VM Options”,然后粘贴:--module-path /PATH/TO/YOUR/JAVAFX/LIB -- add-modules javafx.controls,javafx.fxml,javafx.base,javafx.media(将 /PATH/TO/YOUR/JAVAFX/LIB 替换为您计算机上 javafx 安装目录中“lib”文件夹的目录。这类似于这个官方教程说的是: Create-a-javafx-project-with-IntelliJ ; 但是模块 javafx.media 和 javafx.base 被添加了。

Note that based on my searches and experience, javaFX 11 cannot play .mp3 files (unlike .wav files).请注意,根据我的搜索和经验,javaFX 11无法播放 .mp3 文件(与 .wav 文件不同)。 You may want to consider updating javaFX to 16 to play .mp3 files.您可能需要考虑将 javaFX 更新为 16 以播放 .mp3 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM