Javafx 将声音作为对象播放
Javafx playing sounds as objects
我在用 javafx 做项目时遇到了问题。我正在尝试使用媒体和媒体播放器播放声音,但在选择路径时遇到了问题。我在 IntellijIDEA 上工作。我决定简化工作并创建一个 class sounds.java 来创建对象,该对象采用字符串(路径)和播放和停止声音的方法。(就像我可以多次单击按钮,声音播放一遍又一遍结束)
这是我的代码(由于使用新方法后出错,我将代码放在注释中(
package sample;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;
import static javafx.application.Platform.exit;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
Parent faggot = FXMLLoader.load(getClass().getResource("sample.fxml"));
Font font = Font.font("Times New Roman", FontWeight.BOLD, 48);
System.out.println("Working Directory = " + System.getProperty("user.dir"));
String playClick = System.getProperty("user.dir") + "/src/sounds/select.wav";
String srcMenuMusic = System.getProperty("user.dir") + "/src/sounds/menu.mp3";
String picture = System.getProperty("user.dir") + "/src/minotaur.png";
/* Media click = new Media("select.wav");
Media menuMusic = new Media(new File(srcMenuMusic).toURI().toString());*/
/* MediaPlayer clickPlayer = new MediaPlayer(click);
MediaPlayer menuPlay = new MediaPlayer(menuMusic);*/
Image image = new Image("/sample/minotaur.png");
ImageView view = new ImageView(image);
ImageView view1 = new ImageView(image);
ImageView view2 = new ImageView(image);
ImageView view3 = new ImageView(image);
//Buttons for menu
int x = 0;
Button bStart = new Button("Start");
Button bSettings = new Button("Settings");
Button bAbout = new Button("About");
Button bExit = new Button("Exit");
Button bBack = new Button("Back");
Button bBack2 = new Button("Back");
Button bSound = new Button("Sound: ON");
Button bMusic = new Button("Music: ON");
bStart.setPrefSize(200, 100);
bExit.setPrefSize(200, 100);
bSettings.setPrefSize(200, 100);
bAbout.setPrefSize(200, 100);
bStart.setLayoutY(x+10);
x+=120;
bSettings.setLayoutY(x);
x+=110;
bAbout.setLayoutY(x);
x+=110;
bExit.setLayoutY(x);
bBack.setPrefSize(200, 100);
bBack.setLayoutY(500);
bBack.setLayoutX(200);
bBack2.setPrefSize(200, 100);
bBack2.setLayoutY(500);
bBack2.setLayoutX(200);
bSound.setPrefSize(200, 100);
bSound.setLayoutY(200);
bSound.setLayoutX(200);
bMusic.setPrefSize(200, 100);
bMusic.setLayoutY(200);
bMusic.setLayoutX(500);
String strAbout = "Minotaurus - is a not so funny game about mazes and running away from " +
"minotaur. So far there is not minotaur yet. But I will probably add him on finals. " +
"For it is a simple maze with almost no difficulties. Enjoy your short time wasting!";
Label lAbout = new Label(strAbout);
//lAbout.setPrefSize(10, 20);
lAbout.autosize();
//lAbout.setRotate(270);
lAbout.setFont(Font.font("Cambria", 32));
lAbout.setWrapText(true);
lAbout.setMaxWidth(400);
lAbout.setLayoutX(20);
lAbout.setTextFill(Color.web("#ffffff", 0.8));
Group root = new Group(view,bStart, bExit, bSettings, bAbout);
Group gRoot = new Group(view1);
Group sRoot = new Group(view3, bBack2, bSound, bMusic);
Group aRoot = new Group(view2, bBack, lAbout);
Scene scene = new Scene(root, 1280, 720, Color.BEIGE);
//menuPlay.play();
Scene sGame = new Scene(gRoot, 1280, 720, Color.BEIGE);
Scene sAbout = new Scene(aRoot, 1280, 720, Color.BEIGE);
Scene sSettings = new Scene(sRoot, 1280, 720, Color.BEIGE);
stage.setTitle("Minotaurus");
stage.setScene(scene);
stage.show();
sounds click = new sounds("/sounds/select.wav");
bStart.setOnAction(actionEvent -> {
click.play();
click.player.setOnEndOfMedia(click::stop);
//stage.setScene(sGame);
});
/*bSettings.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(sSettings);
});
bAbout.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(sAbout);
});
bExit.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
exit();
});
});
//aux buttons
bBack.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(scene);
});
bBack2.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(scene);
});*/
//Checking whether sound on or off
AtomicBoolean isPlayingSound = new AtomicBoolean(true);
bSound.setOnAction(actionEvent -> {
click.play();
click.player.setOnEndOfMedia(() -> {
click.stop();
});
if(!isPlayingSound.get()){
isPlayingSound.set(true);
bSound.setText("Sound: ON");
}else{
isPlayingSound.set(false);
bSound.setText("Sound: OFF");
}
});
//Checking whether menu music is playing or not
AtomicBoolean isPlayingMenu = new AtomicBoolean(true);
bMusic.setOnAction(actionEvent -> {
click.play();
click.player.setOnEndOfMedia(() -> {
click.stop();
});
/*if(!isPlayingMenu.get()){
isPlayingMenu.set(true);
menuPlay.play();
bMusic.setText("Music: ON");
}else{
isPlayingMenu.set(false);
menuPlay.pause();
bMusic.setText("Music: OFF");
}*/
});
}
public static void main(String[] args) {
launch(args);
}
}
这里是classsounds.java
package sample;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class sounds {
String fileName;
public static final MediaPlayer player = new MediaPlayer();
/* file:///" + System.getProperty("user.dir").replace('\', '/') + "/*/
sounds(String fileName){
this.fileName = fileName;
Media m = new Media(fileName);
player = new MediaPlayer(m);
}
public void play(){
player.play();
}
public void stop(){
player.stop();
}
}
您需要创建一个声音对象并在您的代码中使用它。像下面这样的东西。请注意对象是如何在 start
方法之外定义的,以便可以轻松引用它:
public class Main extends Application {
//Keep a reference to your Sound object
//Note that methods should ALWAYS start with a capital letter "Sounds"
sounds mySound;
@Override
public void start(Stage stage) throws Exception{
//Create the sound object near the top of your start method
mySound = new sounds(yourFilePathAndName);
//....
bSound.setOnAction(actionEvent -> {
mySound.play();
mySound.player.setOnEndOfMedia(() -> {
mySound.stop();
});
if(!isPlayingSound.get()){
isPlayingSound.set(true);
bSound.setText("Sound: ON");
}else{
isPlayingSound.set(false);
bSound.setText("Sound: OFF");
}
});
//....
}
//....
}
注意:在您的声音 class 中,以下行不能是静态的,并且它不应该是最终的,因为您使用 player = new MediaPlayer(m);
在代码中更改了它。删除此行:
public static final MediaPlayer player = new MediaPlayer();
改为使用以下内容:
public MediaPlayer player;
我在用 javafx 做项目时遇到了问题。我正在尝试使用媒体和媒体播放器播放声音,但在选择路径时遇到了问题。我在 IntellijIDEA 上工作。我决定简化工作并创建一个 class sounds.java 来创建对象,该对象采用字符串(路径)和播放和停止声音的方法。(就像我可以多次单击按钮,声音播放一遍又一遍结束)
这是我的代码(由于使用新方法后出错,我将代码放在注释中(
package sample;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.paint.Color;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;
import static javafx.application.Platform.exit;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
Parent faggot = FXMLLoader.load(getClass().getResource("sample.fxml"));
Font font = Font.font("Times New Roman", FontWeight.BOLD, 48);
System.out.println("Working Directory = " + System.getProperty("user.dir"));
String playClick = System.getProperty("user.dir") + "/src/sounds/select.wav";
String srcMenuMusic = System.getProperty("user.dir") + "/src/sounds/menu.mp3";
String picture = System.getProperty("user.dir") + "/src/minotaur.png";
/* Media click = new Media("select.wav");
Media menuMusic = new Media(new File(srcMenuMusic).toURI().toString());*/
/* MediaPlayer clickPlayer = new MediaPlayer(click);
MediaPlayer menuPlay = new MediaPlayer(menuMusic);*/
Image image = new Image("/sample/minotaur.png");
ImageView view = new ImageView(image);
ImageView view1 = new ImageView(image);
ImageView view2 = new ImageView(image);
ImageView view3 = new ImageView(image);
//Buttons for menu
int x = 0;
Button bStart = new Button("Start");
Button bSettings = new Button("Settings");
Button bAbout = new Button("About");
Button bExit = new Button("Exit");
Button bBack = new Button("Back");
Button bBack2 = new Button("Back");
Button bSound = new Button("Sound: ON");
Button bMusic = new Button("Music: ON");
bStart.setPrefSize(200, 100);
bExit.setPrefSize(200, 100);
bSettings.setPrefSize(200, 100);
bAbout.setPrefSize(200, 100);
bStart.setLayoutY(x+10);
x+=120;
bSettings.setLayoutY(x);
x+=110;
bAbout.setLayoutY(x);
x+=110;
bExit.setLayoutY(x);
bBack.setPrefSize(200, 100);
bBack.setLayoutY(500);
bBack.setLayoutX(200);
bBack2.setPrefSize(200, 100);
bBack2.setLayoutY(500);
bBack2.setLayoutX(200);
bSound.setPrefSize(200, 100);
bSound.setLayoutY(200);
bSound.setLayoutX(200);
bMusic.setPrefSize(200, 100);
bMusic.setLayoutY(200);
bMusic.setLayoutX(500);
String strAbout = "Minotaurus - is a not so funny game about mazes and running away from " +
"minotaur. So far there is not minotaur yet. But I will probably add him on finals. " +
"For it is a simple maze with almost no difficulties. Enjoy your short time wasting!";
Label lAbout = new Label(strAbout);
//lAbout.setPrefSize(10, 20);
lAbout.autosize();
//lAbout.setRotate(270);
lAbout.setFont(Font.font("Cambria", 32));
lAbout.setWrapText(true);
lAbout.setMaxWidth(400);
lAbout.setLayoutX(20);
lAbout.setTextFill(Color.web("#ffffff", 0.8));
Group root = new Group(view,bStart, bExit, bSettings, bAbout);
Group gRoot = new Group(view1);
Group sRoot = new Group(view3, bBack2, bSound, bMusic);
Group aRoot = new Group(view2, bBack, lAbout);
Scene scene = new Scene(root, 1280, 720, Color.BEIGE);
//menuPlay.play();
Scene sGame = new Scene(gRoot, 1280, 720, Color.BEIGE);
Scene sAbout = new Scene(aRoot, 1280, 720, Color.BEIGE);
Scene sSettings = new Scene(sRoot, 1280, 720, Color.BEIGE);
stage.setTitle("Minotaurus");
stage.setScene(scene);
stage.show();
sounds click = new sounds("/sounds/select.wav");
bStart.setOnAction(actionEvent -> {
click.play();
click.player.setOnEndOfMedia(click::stop);
//stage.setScene(sGame);
});
/*bSettings.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(sSettings);
});
bAbout.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(sAbout);
});
bExit.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
exit();
});
});
//aux buttons
bBack.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(scene);
});
bBack2.setOnAction(actionEvent -> {
clickPlayer.play();
clickPlayer.setOnEndOfMedia(() -> {
clickPlayer.stop();
});
stage.setScene(scene);
});*/
//Checking whether sound on or off
AtomicBoolean isPlayingSound = new AtomicBoolean(true);
bSound.setOnAction(actionEvent -> {
click.play();
click.player.setOnEndOfMedia(() -> {
click.stop();
});
if(!isPlayingSound.get()){
isPlayingSound.set(true);
bSound.setText("Sound: ON");
}else{
isPlayingSound.set(false);
bSound.setText("Sound: OFF");
}
});
//Checking whether menu music is playing or not
AtomicBoolean isPlayingMenu = new AtomicBoolean(true);
bMusic.setOnAction(actionEvent -> {
click.play();
click.player.setOnEndOfMedia(() -> {
click.stop();
});
/*if(!isPlayingMenu.get()){
isPlayingMenu.set(true);
menuPlay.play();
bMusic.setText("Music: ON");
}else{
isPlayingMenu.set(false);
menuPlay.pause();
bMusic.setText("Music: OFF");
}*/
});
}
public static void main(String[] args) {
launch(args);
}
}
这里是classsounds.java
package sample;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
public class sounds {
String fileName;
public static final MediaPlayer player = new MediaPlayer();
/* file:///" + System.getProperty("user.dir").replace('\', '/') + "/*/
sounds(String fileName){
this.fileName = fileName;
Media m = new Media(fileName);
player = new MediaPlayer(m);
}
public void play(){
player.play();
}
public void stop(){
player.stop();
}
}
您需要创建一个声音对象并在您的代码中使用它。像下面这样的东西。请注意对象是如何在 start
方法之外定义的,以便可以轻松引用它:
public class Main extends Application {
//Keep a reference to your Sound object
//Note that methods should ALWAYS start with a capital letter "Sounds"
sounds mySound;
@Override
public void start(Stage stage) throws Exception{
//Create the sound object near the top of your start method
mySound = new sounds(yourFilePathAndName);
//....
bSound.setOnAction(actionEvent -> {
mySound.play();
mySound.player.setOnEndOfMedia(() -> {
mySound.stop();
});
if(!isPlayingSound.get()){
isPlayingSound.set(true);
bSound.setText("Sound: ON");
}else{
isPlayingSound.set(false);
bSound.setText("Sound: OFF");
}
});
//....
}
//....
}
注意:在您的声音 class 中,以下行不能是静态的,并且它不应该是最终的,因为您使用 player = new MediaPlayer(m);
在代码中更改了它。删除此行:
public static final MediaPlayer player = new MediaPlayer();
改为使用以下内容:
public MediaPlayer player;