2

I'm trying to mess around with a progressBar for the first time. I want to keep record of the thread using a progressBar --- I did look at this but it didn't help.

Does anyone know how to do this?

I tried using the bytes of the mp3 file to update the progressBar but I was unsuccessful. I want to use the thread which I use to play the mp3 file.

This is a screenshot of the mp3 player.

The mp3 player I'm working on

Adeeb
  • 1,271
  • 3
  • 16
  • 33
  • You already read the tutorial. What can we add to it? How didn't it help? What's the problem? Have you tried something? Where's the code? – JB Nizet Feb 09 '13 at 10:17

2 Answers2

1

The usual problem is, that you also need proper GUI refreshes when updating the progress bar's value. I did async loading with progress bar some time ago, where I used exactly that tutorial.

Note, that with compressed audio files, the usage of byte counters might not be accurate, without doing complete decompression initially, what might not be what you want.

It is essential of course, that you set up the progressbar's value and min & max appropriately using the constructor and/or setMaximum(), setMinimum() and setValue(), before starting to play, e.g.

progressBar.setMinimum(0);
progressBar.setMaximum(totalTime);
progressBar.setValue(0);

but not

progressBar.setValue(elapsedTime);

from your worker thread, use the PropertyChangeListener interface and setProgress() of SwingWorker from within the worker instance as in following example. Just adapt the code to your needs:

http://docs.oracle.com/javase/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java

Sam
  • 7,778
  • 1
  • 23
  • 49
  • I was thinking of monitoring the thread using the thread--- but that seems impossible as it would require one to know how long the thread will run. I decided to use a additional library to get the Id3v2 tag --- use it to find the length of the mp3 file. I will have an extra thread that will keep rescheduling itself using stackoverflow.com/questions/2437141/run-java-thread-at-specific-times to keep updating the progressBar. @Sam Thank You! – Adeeb Feb 09 '13 at 10:51
  • You're welcome. I suggest keeping it simple using the code from that tutorial, since it has to work. Your actual problem seems to be retrieving the playback state. Although Java 7 has/had some bad security holes, you'd have some nice FX classes to play mp3 files with, they provide also accurate playback states: http://stackoverflow.com/a/10237397/1175253 – Sam Feb 09 '13 at 11:00
0

IF HTML5 is allowed you can check the new progress tag: http://www.w3schools.com/tags/tag_progress.asp

Jens Peters
  • 2,075
  • 1
  • 22
  • 30