简体   繁体   English

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();
}

I'm trying to play the sound file, but the error I'm getting is "cannot resolve symbol play" and my IDE also says that "mediaPlayer" is never even used. 我正在尝试播放声音文件,但是出现的错误是“无法解析符号播放”,并且我的IDE也说“ mediaPlayer”从未使用过。 Why is this? 为什么是这样? I'm pretty sure the path to my media is correct (I put it in the root file next to src). 我非常确定我的媒体路径正确(我将其放在src旁边的根文件中)。

This is a class, you need to execute mediaPlayer.play() in your main function of the app. 这是一个类,您需要在应用程序的主要功能中执行mediaPlayer.play()

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

In your main function of the app you play it like this : 在应用程序的主要功能中,您可以像下面这样玩:

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

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

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