So, what I am trying to do is to play a portion or section of the audio between the two thumbs- min and max of the rangeSeekBar. I have used the below library to achieve this feature. https://github.com/YahooArchive/android-range-seek-bar
With some logic from my side, I have achieved the functionality. But I don't know the audio doesn't always stop at the max position. Also, now since I am using custom view, I can't show a progress indicator like where is the currect audio been played. RangeSeekbar doesn't have the below method.
seekbar.setProgress(int duration);
So, its very difficult to see where the music is actually playing. I want a feature something like this.
Can anybody tell me how to achieve this?
Below are the related code.
MainActivity.java
public class MainActivity extends AppCompatActivity {
RangeSeekBar<Integer> mRangeSeekBar;
MediaPlayer mMediaPlayer;
Button playButton;
TextView mEndTextView;
static int max;
SeekBar mSeekBar;
Runnable mRunnable;
Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRangeSeekBar = findViewById(R.id.rangeSeekBar);
mSeekBar = findViewById(R.id.seekBar);
mRangeSeekBar.setNotifyWhileDragging(true);
playButton = findViewById(R.id.play);
mEndTextView = findViewById(R.id.end_duration);
mHandler = new Handler();
mMediaPlayer = MediaPlayer.create(this, R.raw.song);
max = mMediaPlayer.getDuration();
mRangeSeekBar.setRangeValues(0, mMediaPlayer.getDuration());
mSeekBar.setMax(mMediaPlayer.getDuration());
playButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.start();
playProgress();
}
});
mRangeSeekBar.setOnRangeSeekBarChangeListener(new RangeSeekBar.OnRangeSeekBarChangeListener<Integer>() {
@Override
public void onRangeSeekBarValuesChanged(RangeSeekBar<?> bar, Integer minValue, Integer maxValue) {
mMediaPlayer.seekTo(minValue);
max = maxValue;
}
});
}
private void playProgress() {
if(mMediaPlayer.getCurrentPosition() == max) {
mMediaPlayer.stop();
}
if (mMediaPlayer.isPlaying()) {
mRunnable = new Runnable() {
@Override
public void run() {
playProgress();
}
};
mHandler.postDelayed(mRunnable, 0);
}
}
activity_main.xml
<com.yahoo.mobile.client.android.util.rangeseekbar.RangeSeekBar
android:id="@+id/rangeSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
Dependency
implementation 'com.yahoo.mobile.client.android.util.rangeseekbar:rangeseekbar-library:0.1.0'