How to have Time of a song running when playing music.

All your problems with Audio, Video and Images.

How to have Time of a song running when playing music.

Postby Addion » Tue Jan 20, 2009 5:31 am

I can have progressbar running when playing a song on Android. But I want to show time of that music on Text view.

Example
00:34/03:56
This time will be increasing while a song is playing.

Thread background=new Thread(new Runnable() {
public void run() {
try{
while (isRunning){
Thread.sleep(100);
i++;
bar.setProgress(i*100);
if (i%10 == 0){
text = i/10+"/60";
time_.setText((CharSequence)text);
}
}
}catch (Exception e){e.printStackTrace();}
}
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Top

Postby MrSnowflake » Tue Jan 20, 2009 12:55 pm

You didn't say what your problem was, but I suppose it is you are not running the setText() on the ui thread (which you should):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Thread background=new Thread(new Runnable() {
  2.  
  3. public void run() {
  4.  
  5. try{
  6.  
  7. while (isRunning){
  8.  
  9. Thread.sleep(100);
  10.  
  11. i++;
  12.  
  13. if (i%10 == 0){
  14.  
  15. text = i/10+"/60";
  16.  
  17. /* RunOnUiThread() is a member of Activity, so you should call this on your activity */
  18.  
  19. runOnUiThread(new Runnalbe(){
  20.  
  21.   public void run() {
  22.  
  23.     bar.setProgress(i*100);
  24.  
  25.     time_.setText((CharSequence)text);
  26.  
  27.   }
  28.  
  29. });
  30.  
  31. }
  32.  
  33. }
  34.  
  35. }catch (Exception e){e.printStackTrace();}
  36.  
  37. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Addion » Wed Jan 21, 2009 3:55 am

Thank you so much. You totally help me out.
Addion
Developer
Developer
 
Posts: 29
Joined: Tue Nov 18, 2008 3:49 am

Postby MrSnowflake » Wed Jan 21, 2009 11:33 am

Thean I'm happy!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 21 guests