Common Operations for android music players

Source: Internet
Author: User
Tags rewind

1. [Code] variable declaration 01/* variable Declaration */02 private ImageButton playBtn = null; // play and pause 03 private ImageButton latestBtn = null; // last 04 private ImageButton nextButton = null; // next 05 private ImageButton forwardBtn = null; // fast forward to 06 private ImageButton rewindBtn = null; // fast return 07 private TextView playtime = null; // playback time 08 private TextView durationTime = null; // song time 09 private SeekBar seekbar = null; // song progress 10 private Handler handler = null; // used for the progress bar 11 private Handler fHandler = null; // used for fast forward 12 private int currentPosition; // The current playback position 2. [Code] obtain the data from the previous activity 01 @ Override02protected void onCreate (Bundle savedInstanceState) {03 super. onCreate (savedInstanceState); 04 setContentView (R. layout. play); 05 Intent intent = this. getIntent (); 06 Bundle bundle = intent. getExtras (); 07 _ ids = bundle. getIntArray ("_ ids"); // obtain the array 08 position = bundle for saving the music file _ ID. getInt ("position"); // obtain the number of the music to be played. This indicates the number of music to be played. [Code] initialize Control 1 playtime = (TextView) findViewById (R. id. playtime); // display the playback time 2 durationTime = (TextView) findViewById (R. id. duration); // display the total time of the Song 3 playBtn = (ImageButton) findViewById (R. id. playBtn); // the start and pause buttons. 4. latestBtn = (ImageButton) findViewById (R. id. latestBtn); // play the last button 5 nextButton = (ImageButton) findViewById (R. id. nextBtn); // play the next button 6 forwardBtn = (ImageButton) findViewById (R. id. forwardBtn); // fast forward button 7 rewindBtn = (ImageButton) findViewById (R. id. rewindBtn); // return button 8 seekbar = (SeekBar) findViewById (R. id. seekbar); // playback progress bar 4. [Code] define the callback function of each control to jump to [1] [2] [3] [4] [full screen Preview] 001playBtn. setOnClickListener (new View. onClickListener () {// call back 002 @ Override003 public void onClick (View v) {004 if (mp. isPlaying () {// pause 005 pause (); 006 playBtn if the player is playing. setBackgroundResource (007 R. drawable. play_selecor); // change the key status icon 008} else {// Resume playback if no playback is available 009 play (); 010 playBtn. setBackgroundResource (011 R. drawable. pause_selecor); // change the key status icon 012 013} 014} 015}); 016 017latestBtn. setOnClickListener (new View. onClickListener () {// call back 018 @ Override019 public void onClick (View v) {020 int num = _ ids when you click the "Last play" button. length; // get the number of music 021 if (position = 0) {// if it is already the first, play the last 022 position = num-1; 023} else {// otherwise, the first 024 position-= 1; 025} 026 int pos = _ ids [position]; // get the _ ID027 setup () of the music to be played; // prepare for playing 028 play (); // start playing 029} 030}); 031 032nextButton. setOnClickListener (new View. onClickListener () {// call back 033 @ Override034 public void onClick (View v) {035 int num = _ ids when you click "play next. length; // get the number of music 036 if (position = num-1) {// if it is the last one, play the first 037 position = 0; 038} else {039 position + = 1; // otherwise, play the next 040} 041 int pos = _ ids [position]; // get the _ ID042 setup () of the music to be played; // prepare for playing 043 play (); // start playing 044} 045}); 046 047forwardBtn. setOnTouchListener (new OnTouchListener () {// callback 048 @ Override049 public boolean onTouch (View v, MotionEvent event) {050 switch (event. getAction () {051 case MotionEvent. ACTION_DOWN: 052 fHandler. post (forward); // use the handler object to update the progress bar 053 mp. pause (); // when you click the fast forward button, the music stops playing 054 break; 055 056 case MotionEvent. ACTION_UP: 057 fHandler. removeCallbacks (forward); 058 mp. start (); // when the fast forward button is released, the music is temporarily resumed to play 059 playBtn. setBackgroundResource (060 R. drawable. pause_selecor); 061 break; 062} 063 return false; 064} 065}); 066 067rewindBtn. setOnTouchListener (new OnTouchListener () {// call back 068 @ Override069 public boolean onTouch (View v, MotionEvent event) {070 switch (event. getAction () {071 case MotionEvent. ACTION_DOWN: 072 fHandler. post (rewind); 073 mp. pause (); // when you click the rewind button, the music is paused for the moment 074 break; 075 076 case MotionEvent. ACTION_UP: 077 fHandler. removeCallbacks (rewind); 078 mp. start (); // when the quick return button is released, the music will resume playing 079 playBtn. setBackgroundResource (080 R. drawable. pause_selecor); 081 break; 082} 083 return false; 084} 085}); 086 087seekbar. setOnSeekBarChangeListener (new OnSeekBarChangeListener () {088 @ Override089 public void onStopTrackingTouch (SeekBar seekBar) {090 mp. start (); // when you stop dragging the progress bar, the music starts playing 091} 092 @ Override093 public void onStartTrackingTouch (SeekBar seekBar) {094 mp. pause (); // when you start dragging the progress bar, the music stops playing 095} 096 097 @ Override098 public void onProgressChanged (SeekBar seekBar, int progress, 099 boolean fromUser) {100 if (fromUser) {101 mp. seekTo (progress); // when the progress bar value changes, the music player starts playing 102} 103} 104} 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.