0

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.

Enowneb
  • 943
  • 1
  • 2
  • 11

1 Answers1

0

So separate into 2 parts:

Display time remaining and time elapsed of an audio file

So you have a timer that updates the Seekbar every half second :

    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            seekbar.setProgress(mediaPlayer.getCurrentPosition());
        }
    }, 0, 500);

And you have set the OnSeekBarChangeListener, which is expected to be triggered whenever there is a progress change in Seekbar:

    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) {

        }
    });

So basically with the above, you are ready to have clocks displaying the time remaining and the time elapsed. As the onProgressChanged() of your OnSeekBarChangeListener should be triggered by your Timer periodically and therefore you can put some logic there to update your clocks:

// You can define 2 more TextView to display remaining time and elapsed time
TextView tvRemainingTime = findViewById(R.id.tvRemainingTime);
TextView tvElapsedTime = findViewById(R.id.tvElapsedTime);
final int totalDuration = mediaPlayer.getDuration();
seekbar.setMax(totalDuration);
// Hold the timer with a variable so that you can access it if necessary
Timer timer = new Timer();
// Change to run the Timer with a period of 1 second
timer.scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        seekbar.setProgress(mediaPlayer.getCurrentPosition());
    }
}, 0, 1000);

seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        mediaPlayer.seekTo(progress);
        // As progress is in milliseconds, you can write a function to convert milliseconds into displaying time
        tvElapsedTime.setText(convertMillisecondsToTime(progress));
        tvRemainingTime.setText(convertMillisecondsToTime(totalDuration - progress));
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
});

Function to convert milliseconds into displaying time (Or you should further enhance it if you would like to display more detailedly):

private String convertMillisecondsToTime(int target) {
    // For example, 30000 should be displayed as 00:30 mm:ss
    int seconds = (target / 1000) % 60;
    int minutes = (target / 1000) / 60;
    return String.format("%02d:%02d", minutes, seconds);
}

Go to different Activity from different Spinner option

To get selected value of Spinner, you can simply do the following:

String selectedTime = spinner.getSelectedItem().toString();

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 the selectedTime and open the corresponding Activity:

private void openNewActivity() {
    String selectedTime = spinner.getSelectedItem().toString();
    // For example, if the values showing on Spinner are "5 mins", "10 mins", "15 mins"
    if ("5 mins".equals(selectedTime)) {
        Intent intent = new Intent(this, ActivityFiveMinutes.class);
        startActivity(intent);
    } else if ("10 mins".equals(selectedTime)) {
        Intent intent = new Intent(this, ActivityTenMinutes.class);
        startActivity(intent);
    } ...
}

And don't forget to add your newly created Activity in AndroidManifest.xml:

...
        <activity android:name=".ActivityFiveMinutes" />
        <activity android:name=".ActivityTenMinutes" />
...
Enowneb
  • 943
  • 1
  • 2
  • 11