如何在Java中使用MP3音频文件持续时间滑块?
我想用Java制作一个程序来播放MP3文件。我使用JLayer jar播放MP3文件,使用mp3agic jar显示MP3文件标签,如艺术家姓名等。 我想问大家的一件事是,如何在代码中使用JSlider来 使用Slider查找我的MP3文件的持续时间(时间)?如果我必须使用外部JAR,也请让我知道 代码如下:如何在Java中使用MP3音频文件持续时间滑块?,java,Java,我想用Java制作一个程序来播放MP3文件。我使用JLayer jar播放MP3文件,使用mp3agic jar显示MP3文件标签,如艺术家姓名等。 我想问大家的一件事是,如何在代码中使用JSlider来 使用Slider查找我的MP3文件的持续时间(时间)?如果我必须使用外部JAR,也请让我知道 代码如下: import javazoom.jl.decoder.JavaLayerException; import com.mpatric.mp3agic.InvalidDataException
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) {
}
}
首先,您需要知道文件中有多少帧,以及这些帧的总持续时间
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的一种方式是使用媒体/媒体播放器
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);
}
}
除了滑块之外,它包含了所有的成分。因此,您只需要在
状态更改中请求代码。
?您需要知道如何从silder获取值,即getValue(),或者如何将该值转换为时间吗?或者如何用MP3文件寻找到那个时间?我在问如何用MP3文件寻找到那个时间。谢谢你的回答。我会尝试一下,让你知道。我尝试过你的方法,但当我寻找它时,它会抛出我的BoundsException阵列。我还想让滑块随着音乐向前播放而移动。那么我该怎么做呢?你能找到任何值,检查你传递给play
的数字吗?您考虑过使用javafx吗?它似乎有一个更好的API。好的,我可以知道JavaFXMediaPlayer支持哪些格式吗?