1111import android .graphics .Typeface ;
1212import android .graphics .drawable .ColorDrawable ;
1313import android .graphics .drawable .Drawable ;
14- import android .media .MediaMetadataRetriever ;
1514import android .media .MediaPlayer ;
1615import android .media .audiofx .Visualizer ;
1716import android .net .Uri ;
@@ -68,7 +67,7 @@ public class ScPlayerButton extends View {
6867 private Visualizer mVisualizer = null ;
6968
7069 private int mPosition = 0 ;
71- private long mMediaDuration = 0 ;
70+ private int mMediaDuration = 0 ;
7271 private byte [] mWaveToken = null ;
7372 private Rect mDrawingArea = null ;
7473
@@ -202,13 +201,11 @@ private float dipToPixel(float dip) {
202201 * @param source the media path
203202 * @return the duration in milliseconds
204203 */
205- private long getMediaDuration (String source ) {
204+ private int getMediaDuration (String source ) {
206205 try {
207206 // Try to get the media duration
208- MediaMetadataRetriever mmr = new MediaMetadataRetriever ();
209- mmr .setDataSource (this .getContext (), Uri .parse (source ));
210- String durationStr = mmr .extractMetadata (MediaMetadataRetriever .METADATA_KEY_DURATION );
211- return Long .parseLong (durationStr );
207+ MediaPlayer player = MediaPlayer .create (this .getContext (), Uri .parse (source ));
208+ return player .getDuration ();
212209
213210 } catch (Exception ex ) {
214211 // Print the error on the stack and return
@@ -225,11 +222,11 @@ private long getMediaDuration(String source) {
225222 * @param duration the duration in milliseconds
226223 * @return the format time
227224 */
228- private String formatTime (long duration ) {
225+ private String formatTime (int duration ) {
229226 // Get the tokens
230- long seconds = (duration / 1000 ) % 60 ;
231- long minutes = (seconds / 60 ) % 60 ;
232- long hours = (seconds / (60 * 60 )) % 24 ;
227+ int seconds = (duration / 1000 ) % 60 ;
228+ int minutes = (seconds / 60 ) % 60 ;
229+ int hours = (seconds / (60 * 60 )) % 24 ;
233230
234231 // Format
235232 if (hours == 0 )
@@ -394,7 +391,7 @@ private void releaseVisualizer(Visualizer visualizer) {
394391 */
395392 private ScheduledExecutorService initUpdate () {
396393 // Start new one
397- long milliseconds = (long ) ((1 / (float ) ScPlayerButton .UPDATE_FREQUENCY ) * 1000 );
394+ int milliseconds = (int ) ((1 / (float ) ScPlayerButton .UPDATE_FREQUENCY ) * 1000 );
398395 ScheduledExecutorService executor = Executors .newSingleThreadScheduledExecutor ();
399396 executor .scheduleAtFixedRate (new Runnable () {
400397 @ Override
@@ -511,7 +508,7 @@ private Rect drawTime(Canvas canvas, Rect area) {
511508 this .mTimePaint .setTextSize (this .mFontSize );
512509
513510 // Get the time to display
514- long time = this .mMediaDuration ;
511+ int time = this .mMediaDuration ;
515512 if (this .isSelected () &&
516513 this .mPlayer != null && this .mPlayer .isPlaying ())
517514 time = this .mPlayer .getCurrentPosition ();
@@ -746,7 +743,7 @@ protected void onRestoreInstanceState(Parcelable state) {
746743 * @return in milliseconds
747744 */
748745 @ SuppressWarnings ("unused" )
749- public long getDuration () {
746+ public int getDuration () {
750747 return this .mMediaDuration ;
751748 }
752749
0 commit comments