Android uses the seekbar control to control the song progress. androidseekbar

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.