簡體   English   中英

Javafx 將聲音作為對象播放

[英]Javafx playing sounds as objects

我在 javafx 上制作項目時遇到了麻煩。 我正在嘗試使用媒體和媒體播放器播放聲音,但在路徑選擇方面遇到了麻煩。 我在 IntellijIDEA 工作。 我決定簡化工作並創建了一個 class 聲音。java 創建了 object,它采用字符串(路徑)和方法來播放和停止聲音。(就像我可以多次點擊按鈕)

這是我的代碼(由於使用新方法后出現錯誤,我將代碼放在注釋中(

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

這是 class 的聲音。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();
    }
}

您需要創建一個聲音 object 並在您的代碼中使用它。 類似於以下內容。 請注意 object 是如何在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 中,以下行不能是 static,它不應該是最終的,因為您在代碼中使用player = new MediaPlayer(m); . 刪除這一行:

public static final MediaPlayer player = new MediaPlayer();

相反,請使用以下內容:

public MediaPlayer player;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM