Android music player updates playback time and progress bar

Source: Internet
Author: User

The code for updating the playback time and progress bar of the music player in Android is in the packages/audio music/mediaplaybackactivity. Java file.

Source code reference: http://www.oschina.net/code/explore/android-2.2-froyo/com/android/music/MediaPlaybackActivity.java

1. In the onstart () function, start to update the playing time and progress bar.

Call the update time and progress bar operation to determine the time of the next update based on the returned value. The return value determines the interval of the next update.

0505        long next = refreshNow();0506        queueNextRefresh(next);

2. Update the progress bar function refreshnow

The next update interval returned by default is 500 ms.

The interval of the next update is calculated by obtaining the current playback position. After obtaining the current playback position, the value of 1000ms minus the modulus 1000ms is the interval of the next update. Therefore, the interval between the next update cannot exceed 1 second. If you reduce the maximum update interval, you can modify it here. For example, you can modify the value to a maximum of 500 ms:

Long remaining = 500-(Pos % 500 );

Code of the refreshnow function:

1179 private long refreshnow () {1180 if (mservice = NULL) 1181 return 500; // The default return value is 500ms1182 try {1183 long Pos = mposoverride <0? Mservice. position (): mposoverride; // obtain the current playback position 1184 long remaining = 1000-(Pos % 1000); // calculate the interval (within 1 second) of the next update progress bar) 1185 if (Pos> = 0) & (mduration> 0) {1186 mcurrenttime. settext (musicutils. maketimestring (this, POS/1000); // sets the current playback time to 1187 1188 if (mservice. isplaying () {1189 mcurrenttime. setvisibility (view. visible); 1190} else {1191 // blink the counter1192 int vis = mcurrenttime. getvisibilit Y (); 1193 mcurrenttime. setvisibility (vis = view. Invisible? View. visible: View. invisible); 1194 remaining = 500; 1195} 1196 mprogress. setprogress (INT) (1000 * POS/mduration); // set the current progress bar position 1198} else {1199 mcurrenttime. settext ("--: --"); 1200 mprogress. setprogress (1000); 1201} 1202 // return the number of milliseconds until the next full second, so1203 // The counter can be updated at just the right time1204 return remaining; // return the interval of the next update 1205} catch (RemoteException ex) {1206} 1207 return 500; // The default return value is 500ms1208}

The function mservice. Position () for obtaining the current playback position is obtained by calling the position () function of the mediaplaybackservice class.

The position () function of the mediaplaybackservice class calls the position () function of its subclass multiplayer class.

The position () function of the multiplayer class is used to obtain the current playback position by calling the getcurrentposition () function of the mediaplayer class.

3. Send the function queuenextrefresh for the next update.
This function is used to send a message that updates the progress bar after the interval of the next update delay.

1171 private void queuenextrefresh (long delay) {1172 if (! Paused) {1173 message MSG = mhandler. obtainmessage (refresh); // obtain the refresh message 1174 mhandler. removemessages (refresh); // remove unprocessed message 1175 mhandler from the queue. sendmessagedelayed (MSG, delay); // resend the refresh message. After receiving the refresh message, it is called again, so that the 1176} 1177 can be updated cyclically}

 4. Handler processes asynchronous message refresh
Use handler to process asynchronous messages and update the playback time and progress bar of the main thread.

1210 private final handler mhandler = new handler () {1211 @ override1212 public void handlemessage (Message MSG) {1213 switch (MSG. what) {1214 case album_art_decoded: 1215 malbum. setimagebitmap (Bitmap) MSG. OBJ); 1216 malbum. getdrawable (). setdither (true); 1217 break; 1218 1219 case Refresh: 1220 long next = refreshnow (); // update the progress bar of the music playback interface 1221 queuenextrefresh (next ); // interval of the next update progress bar. After the next delay, the update progress bar is 1222 break;

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.