Skip to content

Commit 92beebe

Browse files
author
paroca72
committed
- Changed duration way calculation
- Changed durection from long to int
1 parent 5f489f5 commit 92beebe

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For customize the component please read guide below.
1818

1919
#### Public methods
2020

21-
- **long getDuration()**<br />
21+
- **int getDuration()**<br />
2222
Get back the media duration.
2323

2424
- **boolean isPlaying()**<br />
@@ -93,7 +93,7 @@ Add the dependency
9393
```java
9494
dependencies {
9595
...
96-
compile 'com.github.paroca72:sc-player:1.0.1'
96+
compile 'com.github.paroca72:sc-player:1.0.2'
9797
}
9898
```
9999

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
defaultConfig {
1010
minSdkVersion 15
1111
targetSdkVersion 25
12-
versionCode 10
13-
versionName '1.0.1'
12+
versionCode 13
13+
versionName '1.0.2'
1414
vectorDrawables.useSupportLibrary = true
1515
}
1616
buildTypes {

library/src/main/java/com/sccomponents/playerbutton/ScPlayerButton.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import android.graphics.Typeface;
1212
import android.graphics.drawable.ColorDrawable;
1313
import android.graphics.drawable.Drawable;
14-
import android.media.MediaMetadataRetriever;
1514
import android.media.MediaPlayer;
1615
import android.media.audiofx.Visualizer;
1716
import 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

Comments
 (0)