Common android music player operations

Source: Internet
Author: User
Tags rewind
/* Variable Declaration */private ImageButton playBtn = null; // play and pause private ImageButton latestBtn = null; // The last private ImageButton nextButton = null; // The next private ImageButton forwardBtn = null; // fast forward to private ImageButton rewindBtn = null; // return private TextView playtime = null; // private TextView durationTime = null; // song time private SeekBar seekbar = null; // song progress private Handler handler = null; // used for the progress bar private Handler fHandler = null; // used for fast-forward private int currentPosition; // current playback position/* obtain the data transmitted from the list */@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. play); Intent intent = this. getIntent (); Bundle bundle = intent. getExtras (); _ ids = bundle. getIntArray ("_ ids"); // obtain the array position = bundle for saving the music file _ ID. getInt ("position"); // obtain the number of the music to be played, which indicates the number of the music to be played. // The code is incomplete, see the following code}/* initialization control */playtime = (TextView) findViewById (R. id. playtime); // display the played time durationTime = (TextView) findViewById (R. id. duration); // display the total time playBtn = (ImageButton) findViewById (R. id. playBtn); // the start and pause buttons latestBtn = (ImageButton) findViewById (R. id. latestBtn); // play the last button nextButton = (ImageButton) findViewById (R. id. nextBtn); // play the next button forwardBtn = (ImageButton) findViewById (R. id. forwardBtn); // rewindBtn = (ImageButton) findViewById (R. id. rewindBtn); // the return button seekbar = (SeekBar) findViewById (R. id. seekbar); // playback progress bar/* defines the callback function of each control */playBtn. setOnClickListener (new View. onClickListener () {// callback @ Overridepublic void onClick (View v) {if (mp. isPlaying () {// pause (); playBtn if playing is in progress. setBackgroundResource (R. drawable. play_selecor); // change the key status icon} else {// resume play () If no playback is available; playBtn. setBackgroundResource (R. drawable. pause_selecor); // change the button status icon }}); latestBtn. setOnClickListener (new View. onClickListener () {// callback @ Overridepublic void onClick (View v) {int num = _ ids when you click the "Last play" button. length; // get the number of music if (position = 0) {// if it is already the first, play the last position = num-1 ;} else {// otherwise, the previous position-= 1;} int pos = _ ids [position]; // get the _ IDsetup () of the music to be played (); // prepare for playing play (); // start playing}); nextButton. setOnClickListener (new View. onClickListener () {// when you click "play next", the callback @ Overridepublic void onClick (View v) {int num = _ ids. length; // obtain the number of music if (position = num-1) {// if it is the last one, play the first position = 0;} else {position + = 1; // otherwise, play the next one} int pos = _ ids [position]; // get the _ IDsetup () of the music to be played; // prepare for playing play (); // start playing}); forwardBtn. setOnTouchListener (new OnTouchListener () {// callback @ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: fHandler. post (forward); // use the handler object to update the progress bar mp. pause (); // when you click the quick forward button, the music is paused for break; case MotionEvent. ACTION_UP: fHandler. removeCallbacks (forward); mp. start (); // when the quickforward button is released, the playBtn will resume playing. setBackgroundResource (R. drawable. pause_selecor); break;} return false ;}}); rewindBtn. setOnTouchListener (new OnTouchListener () {// callback @ Overridepublic boolean onTouch (View v, MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: fHandler. post (rewind); mp. pause (); // when the quick return button is clicked, the break of the music is temporarily paused; case MotionEvent. ACTION_UP: fHandler. removeCallbacks (rewind); mp. start (); // when the quick return button is released, the playBtn of the music will be resumed for the moment. setBackgroundResource (R. drawable. pause_selecor); break;} return false ;}}); seekbar. setOnSeekBarChangeListener (new OnSeekBarChangeListener () {@ Overridepublic void onStopTrackingTouch (SeekBar seekBar) {mp. start (); // when you stop dragging the progress bar, the music starts to play.} @ Overridepublic void onStartTrackingTouch (SeekBar seekBar) {mp. pause (); // when you start dragging the progress bar, the music is paused.} @ Overridepublic void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) {if (fromUser) {mp. seekTo (progress); // when the progress bar value changes, the music player starts playing from the new position }}});
Related Article

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.