Playing progress bar of the android player

Source: Internet
Author: User

Playing music is controlled by mediaplayer. The progress bar of the progress bar can be used to display the playing progress. You can also use the slider of the seekbar to control playing music. One button is used to play the song start thread, the other cancel thread and stop playing the song.

Seekbar:

Set the slider position to setprogress (INT value );

Set the maximum value to setmax (INT value );

 

Musicplayer:

The method for playing music is: start ();

Stop ();

Set the playing position to seekto (INT value );

The length of the obtained music is getduration ();

Get the current playback position: getcurrentposition ();

 

Handler:

Start thread: Post (runnable );

Cancel thread: removecallbacks (runnable );

Delayed startup thread (Time in milliseconds): postdelayed (runnable, int time );

 

The Code is as follows:

Package COM. android. test; <br/> Import android. app. activity; <br/> Import android. media. mediaplayer; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. view. view; <br/> Import android. widget. button; <br/> Import android. widget. seekbar; <br/> public class test extends activity {</P> <p> private button button1; <br/> private button button2; <br/> private seekbar seekbar1; <br/> private mediaplayer player; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> Init (); <br/> findview (); <br/> setlistener (); <br/>}</P> <p> private void Init () {<br/> player = mediaplayer. create (this, R. drawable. example); <br/>}< br/> private void findview () {<br/> button1 = (button) findviewbyid (R. id. button1); <br/> button2 = (button) findviewbyid (R. id. button2); <br/> seekbar1 = (seekbar) findviewbyid (R. id. seekbar1); <br/> // obtain the length of the Song and set it to the maximum value of the playback progress bar. <br/> seekbar1.setmax (player. getduration (); <br/>}</P> <p> handler = new handler (); <br/> runnable updatethread = new runnable () {<br/> Public void run () {<br/> // obtain the current playing position of the Song and set it to the value of the playback progress bar <br/> seekbar1.setprogress (player. getcurrentposition (); <br/> // start the thread after a delay of 100 milliseconds each time <br/> handler. postdelayed (updatethread, 100); <br/>}< br/>}; </P> <p> private void setlistener () {<br/> button1.setonclicklistener (New button. onclicklistener () {<br/> @ override <br/> Public void onclick (view arg0) {<br/> player. start (); <br/> // start <br/> handler. post (updatethread); <br/>}< br/>}); <br/> button2.setonclicklistener (New button. onclicklistener () {<br/> @ override <br/> Public void onclick (view arg0) {<br/> player. stop (); <br/> // cancel the thread <br/> handler. removecallbacks (updatethread); <br/>}< br/>}); <br/> seekbar1.setonseekbarchangelistener (New seekbar. onseekbarchangelistener () {<br/> @ override <br/> Public void onprogresschanged (seekbar, int progress, <br/> Boolean fromuser) {<br/> // fromuser determines that the slider value is changed by the user <br/> If (fromuser = true) {<br/> player. seekto (Progress); <br/>}< br/> @ override <br/> Public void onstarttrackingtouch (seekbar) {<br/> // todo auto-generated method stub <br/>}< br/> @ override <br/> Public void onstoptrackingtouch (seekbar) {<br/> // todo auto-generated method stub <br/>}< br/>}); <br/>}</P> <p>}

Is:

 

 

Seekbar is bound to the setonseekbarchangelistener () event. The first of the three methods is called when the slider slides, the second is called when the sliding starts, and the third is called when the sliding stops.

I hope you can communicate with me more! Now, we are here today!

 

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.