2

该程序是一个音乐播放器,允许用户从剪辑对象和音频输入流中选择 .wav 文件、播放、暂停、恢复和重新启动音乐文件。音频输入流加载由用户通过 FileChooser 确定的文件。程序可以通过选择文件、按播放、暂停然后再次播放来播放、暂停和恢复,但不能使用重新启动方法或通过相应按钮调用的恢复方法进行播放。相反,程序会挂起,直到单击 X 按钮。我认为这与 resetaudiostream 方法有关,但我不确定是什么。可能与结束旧剪辑并创建新剪辑实例有关。请检查逻辑,让我知道是什么导致它挂起以及如何补救。

包装样品;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;


public class Main extends Application {
    static File musicfile;
    static Long currentFrame;
    static Clip clip;
    static String status = "play";
    static AudioInputStream audioInputStream;
    static String filePath;
    public void SimpleAudioPlayer()
            throws UnsupportedAudioFileException,
            IOException, LineUnavailableException
    {
        // create AudioInputStream object
        audioInputStream =
                AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());

        // create clip reference
        clip = AudioSystem.getClip();

        // open audioInputStream to the clip
        clip.open(audioInputStream);

        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Music Player");
        GridPane gp = new GridPane();

        Button selectFile = new Button("Select File");
        GridPane.setConstraints(selectFile, 0,0);
        selectFile.setOnAction(event->{
            FileChooser filechooser = new FileChooser();
            // create AudioInputStream object
            try {
                musicfile = filechooser.showOpenDialog(null);
                audioInputStream = AudioSystem.getAudioInputStream(musicfile);

                clip = AudioSystem.getClip();

                // open audioInputStream to the clip
                clip.open(audioInputStream);

            }catch(IOException | UnsupportedAudioFileException | LineUnavailableException e){
                e.printStackTrace();
            }
        });
        Button play = new Button("Play");
        GridPane.setConstraints(play, 1,0);
        play.setOnAction(event->{
            if(status == "play") {


                    clip.loop(Clip.LOOP_CONTINUOUSLY);

            }
            play();

        });
        Button pause = new Button("Pause");
        GridPane.setConstraints(pause, 2,0);
        pause.setOnAction(event -> pause());

        Button restart = new Button("Restart");
        GridPane.setConstraints(restart, 0,1);
        restart.setOnAction(event -> {
            try{
            restart();
        }
        catch(IOException | UnsupportedAudioFileException | LineUnavailableException e){
        e.printStackTrace();}
        });
        Button resume = new Button("Resume");
        GridPane.setConstraints(resume, 1,1);
        resume.setOnAction(event -> {
            try {
                resumeAudio();
            }catch(IOException | LineUnavailableException | UnsupportedAudioFileException e){
                e.printStackTrace();
        }
        });
        gp.getChildren().addAll(play,selectFile, pause, restart, resume);
        primaryStage.setScene(new Scene(gp, 300, 275));
        primaryStage.show();
    }

    public void play()
    {
        //start the clip
        clip.start();

        status = "play";
    }

    // Method to pause the audio
    public  void pause()
    {
        if (status.equals("paused"))
        {
            System.out.println("audio is already paused");
            return;
        }
        currentFrame =
                clip.getMicrosecondPosition();
        clip.stop();
        status = "paused";
    }

    // Method to resume the audio
    public void resumeAudio() throws UnsupportedAudioFileException,
            IOException, LineUnavailableException
    {
        if (status.equals("play"))
        {
            System.out.println("Audio is already "+
                    "being played");
            return;
        }
        clip.close();
        resetAudioStream();
        clip.setMicrosecondPosition(currentFrame);
        status = "play";
        play();
    }

    // Method to restart the audio
    public void restart() throws IOException, LineUnavailableException,
            UnsupportedAudioFileException
    {
        clip.stop();
        clip.close();
        resetAudioStream();
        currentFrame = 0L;
        clip.setMicrosecondPosition(0);
        status = "play";
        play();
    }

    // Method to stop the audio
    public void stop() throws UnsupportedAudioFileException,
            IOException, LineUnavailableException
    {
        currentFrame = 0L;
        clip.stop();
        clip.close();
    }

    // Method to jump over a specific part
    public void jump(long c) throws UnsupportedAudioFileException, IOException,
            LineUnavailableException
    {
        if (c > 0 && c < clip.getMicrosecondLength())
        {
            clip.stop();
            clip.close();
            resetAudioStream();
            currentFrame = c;
            clip.setMicrosecondPosition(c);
            this.play();
        }
    }

    // Method to reset audio stream
    public void resetAudioStream() throws UnsupportedAudioFileException, IOException,
            LineUnavailableException
    {
        audioInputStream = AudioSystem.getAudioInputStream(musicfile);
        clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
    }
    public static void main(String[] args) {
        launch(args);
    }
}
4

1 回答 1

1

使用以下命令获得所需的功能非常简单MediaPlayer

import java.net.URI;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.GridPane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaPlayer.Status;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {

    private MediaPlayer player;
    private static final long JUMP_BY = 5000;//millis 

    @Override
    public void start(Stage primaryStage) throws Exception{

        URI uri = new URI("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");
        Media media = new Media(uri.toString());
        //OR Media media = new Media("http://traffic.libsyn.com/dickwall/JavaPosse373.mp3");
        player = new MediaPlayer(media);
        player.setOnError(() -> System.out.println(media.getError().toString()));

        GridPane gp = new GridPane();
        gp.setHgap(10);

        Button play = new Button("Play");
        GridPane.setConstraints(play, 0,0);
        play.setOnAction(event->  playAudio());

        Button pause = new Button("Pause");
        GridPane.setConstraints(pause, 1,0);
        pause.setOnAction(event -> pauseAudio());

        Button resume = new Button("Resume");
        GridPane.setConstraints(resume, 2,0);
        resume.setOnAction(event -> resumeAudio());

        Button stop = new Button("Stop");
        GridPane.setConstraints(stop, 3,0);
        stop.setOnAction(event ->  stopAudio());

        Button restart = new Button("Restart");
        GridPane.setConstraints(restart, 4,0);
        restart.setOnAction(event ->  restartAudio());

        Button jump = new Button("Jump >");
        GridPane.setConstraints(jump, 5,0);
        jump.setOnAction(event ->  jump(JUMP_BY));

        Label time = new Label();
        GridPane.setConstraints(time, 6,0);
        time.textProperty().bind( player.currentTimeProperty().asString("%.4s") );

        gp.getChildren().addAll(play, pause, resume, stop, restart, jump, time);
        primaryStage.setScene(new Scene(gp, 400, 45));
        primaryStage.show();
    }

    //play audio 
    public void playAudio()
    {
        player.play();
    }

    //pause audio
    public  void pauseAudio()
    {
        if (player.getStatus().equals(Status.PAUSED))
        {
            System.out.println("audio is already paused");
            return;
        }
        player.pause();
    }

    //resume audio
    public void resumeAudio()
    {
        if (player.getStatus().equals(Status.PLAYING))
        {
            System.out.println("Audio is already playing");
            return;
        }
        playAudio();
    }

    //restart audio
    public void restartAudio()
    {
        player.seek(Duration.ZERO);
        playAudio();
    }

    // stop audio
    public void stopAudio()
    {
       player.stop();
    }

    //jump by c millis 
    public void jump(long c)
    {
        player.seek(player.getCurrentTime().add(Duration.millis(c)));
    }

    public static void main(String[] args) {
        launch(args);
    }
}
于 2020-05-19T07:10:02.433 回答