Android uses the seekbar control to control the song progress. androidseekbar
I am a newbie. I used the seekbar control in android to control the progress of the song and the seekbar program that automatically moves with the play of the song, I hope you can give me some advice ······
Let's talk a little bit about it. paste a program diagram first:
The interface is not very nice and has not been beautified. Let's take a look at it.
The main idea is to use the thread to display the position of the seekbar control every second (this is to keep the progress bar changing with the progress of the song. If you want to increase the progress bar more continuously, you can change the running time of a program to make it faster)
I only took out the degree of seekbar:
Private int position = 0; // the position where the media music is played
Private SeekBar seekbar;
Public Handler seekbarhandler = null;
Public seek1_read seek1_read = null;
// First define the thread method.
Seek1_read = new seek1_read ();
Public class seek1_read extends Thread {
Private SeekBar seekbar;
Public void run (){
Message message = new Message ();
Message. what = 1;
Seekbarhandler. sendMessage (message );
}
}
// Define handler to handle the event
Seekbarhandler = new Handler (){
Public void handleMessage (Message msg ){
Super. handleMessage (msg );
If (msg. what = 1 ){
Seekbar. setProgress (int) (double) mediaplayer. getCurrentPosition ()/mediaplayer. getDuration () * 100 ));
Seekbarhandler. postDelayed (seekreceivread, 1000 );
}
}
};
// Define and listen to events for seekbar events:
Seekbar = (SeekBar) findViewById (R. id. seekbar1 );
Seekbar. setOnSeekBarChangeListener (new OnSeekBarChangeListener (){
@ Override
Public void onProgressChanged (SeekBar arg0, int arg1, boolean arg2) {// numeric value change
Position = arg1;
Mediaplayer. start ();
}
@ Override
Public void onStartTrackingTouch (SeekBar arg0) {// start dragging
}
@ Override
Public void onStopTrackingTouch (SeekBar arg0) {// stop dragging
Mediaplayer. seekTo (int) (double) position/100) * mediaplayer. getDuration (); // The default maximum value of seekbar is 100, so 100 is used here, but this makes the value not very accurate. I didn't come up with it.
// Other solutions to make them more accurate, so this method is only available for the time being.
}
});
Basically, this is the way to use it. For the first time I wrote a blog, I would like to ask more comments if there are any bad things. I will make some corrections later.