如何在Java中使用Slider for MP3音频文件持续时间?

时间:2017-07-17 10:31:33

标签: java

我想用Java编写一个程序来播放MP3文件。我使用JLayer jar播放MP3文件和mp3agic jar来显示mp3文件标签,如艺术家姓名等。 我想问你们的一件事是我如何在我的代码中使用JSlider 使用Slider寻找我的MP3文件的持续时间(时间)?如果我必须使用外部JAR,请告诉我

以下是代码:

import javazoom.jl.decoder.JavaLayerException;
import com.mpatric.mp3agic.InvalidDataException;
import com.mpatric.mp3agic.Mp3File;
import com.mpatric.mp3agic.UnsupportedTagException;
import javazoom.jl.player.advanced.*;
import java.awt.FileDialog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class MP3Player extends JFrame implements ChangeListener{
JFrame f;
JSlider sl;
FileInputStream fis;
AdvancedPlayer pl;
Mp3File mp3tags;
String s;
MP3Player() throws JavaLayerException,IOException, UnsupportedTagException, InvalidDataException
{
    f=new JFrame();
    f.setTitle("MP3 Player");
    f.setBounds(250, 150, 500, 500);
FileDialog fd=new FileDialog(f,"Select Audio File",FileDialog.LOAD);
fd.setVisible(true);
FileInputStream fis=new FileInputStream(new File(fd.getDirectory(),fd.getFile()));
pl=new AdvancedPlayer(fis);
s=fd.getDirectory()+fd.getFile();
mp3tags=new Mp3File(s);
System.out.println(mp3tags.getId3v2Tag().getArtist());
new Thread()
{
    public void run()
    {
        try {
            pl.play();
        } catch (JavaLayerException e) {
            e.printStackTrace();
        }
    }
}.start();
sl=new JSlider();
sl.setBounds(250, 500, 500, 500);
f.add(sl);
sl.setMinimum(0);
sl.setValue(0);
sl.addChangeListener(this);
}
public static void main(String asrar[]) throws JavaLayerException,IOException, UnsupportedTagException, InvalidDataException
{
    MP3Player mp3=new MP3Player();
    mp3.f.setVisible(true);
}
@Override
public void stateChanged(ChangeEvent e) {

}
}

1 个答案:

答案 0 :(得分:0)

首先,您需要知道文件中有多少帧,以及这些帧的总持续时间。

int totalFrames = mp3Tags.getFrameCount();
long timeInMillis = mp3Tags.getLengthInMilliseconds();

一旦我们拥有了这一点,就可以做很多事情。您可以设置滑块的最大值。

sl.setMaximumValue(totalFrames);

然后当您移动滑块时,您可以让玩家从该点开始播放。

@Override
public void stateChanged(ChangeEvent e) {
    int value = ((JSlider)e.getSource()).getValue();
    pl.play(value, totalFrames); #or Integer.MAX_VALUE to play to end for certain.
}

javafx方式是使用Media / MediaPlayer。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.nio.file.Paths;

/**
 * Created by melkor on 7/30/17.
 */
public class PlayMediaFx extends Application {
    MediaPlayer player;
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane root = new Pane();
        Pane pane = new VBox();

        Button button = new Button("play");
        button.setOnMouseClicked(evt->{
            if(player==null){
                loadSound();
            }
            player.play();
        });

        Button seek = new Button("seek 3 seconds");
        seek.setOnMouseClicked(evt->{
            if(player!=null){
                Duration d = player.getCurrentTime();
                player.seek(d.add(new Duration(3000)));
            }
        });

        Button pause = new Button("pause");
        pause.setOnMouseClicked(evt->{
            if(player!=null){

                player.pause();
            }
        });

        Button stop = new Button("stop");
        stop.setOnMouseClicked(evt->{
            if(player!=null){
                player.stop();
            }
        });

        pane.getChildren().addAll(button, pause, seek, stop);
        root.getChildren().addAll(pane);
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public void loadSound(){
        Media media = new Media(Paths.get("test.aif").toUri().toString());
        player = new MediaPlayer(media);
        player.setOnReady(()->{
            player.setStopTime(player.getTotalDuration());
        });
    }

    public static void main(String[] args){
        launch(args);
    }
}

除滑块外,它有所有成分。