java 为媒体播放器制作一个搜索栏。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12786346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a seek bar for media player.
提问by Haxor
I am making a media player using JMF, I want to use my own control components Can anyone please help me in making a seek bar for media player so that it can play song according to the slider position.
我正在使用 JMF 制作媒体播放器,我想使用自己的控制组件 谁能帮我制作媒体播放器的搜索栏,以便它可以根据滑块位置播放歌曲。
Just suggest me some logic, I can figure out the coding part afterwards
给我建议一些逻辑,我可以在之后弄清楚编码部分
if(player!=null){
long durationNanoseconds =
(player.getDuration().getNanoseconds());
durationbar.setMaximum((int) player.getDuration().getSeconds());
int duration=(int) player.getDuration().getSeconds();
int percent = durationbar.getValue();
long t = (durationNanoseconds / duration) * percent;
Time newTime = new Time(t);
player.stop();
player.setMediaTime(newTime);
player.start();
mousedrag=true;
Here is the code. Now how can I make the slider move along with the song? Slider works when I drag/click on it, but it doesn't move with the song.
这是代码。现在我怎样才能让滑块随着歌曲移动?当我拖动/单击它时,滑块可以工作,但它不会随着歌曲移动。
回答by Andrew Thompson
The problem with using a slider for this is that when the slider position is moved programmatically, it fires events. When an event is fired on a slider, it typically means the app. has to do something, such as move the song position. The effect is a never ending loop. There is probably a way around this by setting flags and ignoring some events, but I decided to go a different way.
为此使用滑块的问题在于,当以编程方式移动滑块位置时,它会触发事件。当在滑块上触发事件时,通常表示应用程序。必须做一些事情,例如移动歌曲位置。效果是一个永无止境的循环。通过设置标志并忽略某些事件,可能有一种解决方法,但我决定采用不同的方式。
Instead I used a JProgressBar
to indicate the location in the track, and a MouseListener
to detect when the user clicks on a separate position. Update the progress bar use a Swing Timer
that checks the track location every 50-200 milliseconds. When a MouseEvent
is detected, reposition the track.
相反,我使用 aJProgressBar
来指示轨道中的位置,并使用 aMouseListener
来检测用户何时单击单独的位置。使用Timer
每 50-200 毫秒检查轨道位置的 Swing 更新进度条。当MouseEvent
检测到a时,重新定位轨道。
The bar can be seen in the upper right of this GUI. Hovering over it will produce a tool tip showing the time in the track at that mouse position.
可以在此 GUI 的右上角看到该栏。将鼠标悬停在它上面会产生一个工具提示,显示在该鼠标位置的轨迹中的时间。
回答by Suraj Chandran
You could use a JSlider.
您可以使用 JSlider。
You can learn more from the Slider tutorial
您可以从Slider 教程中了解更多信息
回答by Rempelos
You don't have to revalidate
the container in order to change the slider.
您不必revalidate
为了更改滑块而使用容器。
Use these lines each time a new player is created:
每次创建新玩家时使用这些行:
slider.setMinimum(0);
slider.setMaximum(duration);
slider.setValue(0);
new UpdateWorker(duration).execute();
where duration
is the variable holding the duration of the song in seconds.
其中duration
是保持歌曲持续时间(以秒为单位)的变量。
And here is the code (used as inner class) which updates the slider:
这是更新滑块的代码(用作内部类):
private class UpdateWorker extends SwingWorker<Void, Integer> {
private int duration;
public UpdateWorker(int duration) {
this.duration = duration;
}
@Override
protected Void doInBackground() throws Exception {
for (int i = 1; i <= duration; i++) {
Thread.sleep(1000);
publish(i);
}
return null;
}
@Override
protected void process(List<Integer> chunks) {
slider.setValue(chunks.get(0));
}
}
Now the slider will move to the right until the end of the song.
现在滑块将向右移动直到歌曲结束。
Also note that unless you want to use a custom slider, JMF provides a simple (and working) slider via player.getVisualComponent()
(see this example).
另请注意,除非您想使用自定义滑块,否则 JMF 提供了一个简单(且有效)的滑块 via player.getVisualComponent()
(请参阅此示例)。
UPDATE
更新
In order to pause/resume the worker thread (and thus the slider and the song), here is an example with a button that sets the appropriate flags.
为了暂停/恢复工作线程(以及滑块和歌曲),这里是一个带有设置适当标志的按钮的示例。
private boolean isPaused = false;
JButton pause = new JButton("Pause");
pause.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JButton source = (JButton)e.getSource();
if (!isPaused) {
isPaused = true;
source.setText("Resume");
} else {
isPaused = false;
source.setText("Pause");
}
}
});
The method doInBackground
should be changed to something like that:
该方法doInBackground
应更改为如下所示:
@Override
protected Void doInBackground() throws Exception {
for (int i = 0; i <= duration; i++) {
if (!isPaused) {
publish(i);
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
}
while (isPaused) {
try {
Thread.sleep(50);
continue;
} catch(InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
Modify it accordingly to pause/resume the song along with the slider.
相应地修改它以与滑块一起暂停/恢复歌曲。
You should also consider @AndrewThompson's answer.
您还应该考虑@AndrewThompson 的回答。