I have a seek bar already to an audio file I have in my raw file with pause/play/stop functionality, but how would I also add "clock", for lack of a better word, that shows how much time remains, or how much has gone by already based on the audio too?
And on one of my activities, I have a drop down option with different times on it, 5,10,15 mins, how can I make it so that when the user chooses one of these options, a different audio plays or so that it goes to a separate activity page based on what the user chose?
Here is my current activity with the audio:
public void play(View view){
mediaPlayer.start();
}
public void pause(View view){
mediaPlayer.pause();
}
public void stop(View view){
mediaPlayer.stop();
mediaPlayer.reset();
}
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
seekbar = findViewById(R.id.seekBar);
mediaPlayer = MediaPlayer.create(this, R.raw.mixkit);
button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openNewActivity();
}
});
seekbar.setMax(mediaPlayer.getDuration());
new Timer().scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
seekbar.setProgress(mediaPlayer.getCurrentPosition());
}
}, 0, 500);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
mediaPlayer.seekTo(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
And for my page with dropdown:
Spinner spinner;
Button startButton;
Button returnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
spinner = findViewById(R.id.spinner);
ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this, R.array.time, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
startButton = findViewById(R.id.button2);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openNewActivity();
}
});
returnButton = findViewById(R.id.button3);
returnButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
returnToMain();
}
});
}
I've spent a long time googling and havent found anything regarding the clock, only how to implement a clock and timer, but not how to link it with the seekbar/audio file. I assume I could make a clock and find out how long audio is but I feel like there must be a better way.
So separate into 2 parts:
Display time remaining and time elapsed of an audio file
So you have a timer that updates the
Seekbarevery half second :And you have set the
OnSeekBarChangeListener, which is expected to be triggered whenever there is a progress change inSeekbar:So basically with the above, you are ready to have clocks displaying the time remaining and the time elapsed. As the
onProgressChanged()of yourOnSeekBarChangeListenershould be triggered by yourTimerperiodically and therefore you can put some logic there to update your clocks:Function to convert milliseconds into displaying time (Or you should further enhance it if you would like to display more detailedly):
Go to different Activity from different Spinner option
To get selected value of Spinner, you can simply do the following:
And of course you can get the value of Spinner by index or by another way. See more here.
So for your
openNewActivity()function, you can check theselectedTimeand open the corresponding Activity:And don't forget to add your newly created Activity in AndroidManifest.xml: