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.
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.
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 aMouseListener
to detect when the user clicks on a separate position. Update the progress bar use a SwingTimer
that checks the track location every 50-200 milliseconds. When aMouseEvent
is detected, reposition the track.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.
You could use a JSlider.
You can learn more from the Slider tutorial
You don't have to
revalidate
the container in order to change the slider.Use these lines each time a new player is created:
where
duration
is the variable holding the duration of the song in seconds.And here is the code (used as inner class) which updates the slider:
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).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.
The method
doInBackground
should be changed to something like that:Modify it accordingly to pause/resume the song along with the slider.
You should also consider @AndrewThompson's answer.