JavaFX - MediaPlayer 不工作

JavaFX - MediaPlayer Not Working

package main;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.File;
import javafx.scene.media.AudioClip;


public class Controller {
Media sound = new Media("mouseHover.mp3");
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();
}

我正在尝试播放声音文件,但我得到的错误是 "cannot resolve symbol play" 并且我的 IDE 还说 "mediaPlayer" 从未被使用过。为什么是这样?我很确定我的媒体路径是正确的(我把它放在 src 旁边的根文件中)。

这是一个class,您需要在应用程序的主要功能中执行mediaPlayer.play()

public class Controller {
    Media sound = new Media("mouseHover.mp3");
    MediaPlayer mediaPlayer = new MediaPlayer(sound);
    //Empty constructor
    public Controller()
    {
    }
}

在应用程序的主要功能中,您可以这样玩:

public static void main(String [] args)
{
       Controller ct = new Controller();
       ct.mediaPlayer.play();
}