Gio Vanno
Gio Vanno

Reputation: 13

play mp3 files at designated duration

I'm trying to make an app that can play mp3 files at designated duration through MediaPlayer

for example:

Start Time: 00:42

End Time: 01:23

my question is:

-is it possible to designate the start and end time via textview at MM:SS format?

Thanks!!

Upvotes: 0

Views: 195

Answers (2)

Jaydeep Khambhayta
Jaydeep Khambhayta

Reputation: 5279

use This code

 MediaPlayer mp;

Runnable stopPlayerTask = new Runnable(){
    @Override
    public void run() {
        mp.pause();
    }};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    EditText etStartTime = (EditText)findViewById(R.id.edittext_start_time);
    EditText etEndTime = (EditText)findViewById(R.id.edittext_end_time);

    String StartTime = etStartTime.getText().toString();
    String EndTime = etEndTime.getText().toString();

    long min = Integer.parseInt(StartTime .substring(0, 2));
    long sec = Integer.parseInt(StartTime .substring(3));

    long minEnd = Integer.parseInt(StartTime .substring(0, 2));
    long secEnd = Integer.parseInt(StartTime .substring(3));

    long startLong = (min * 60L) + sec;
    long endLong = (minEnd * 60) + secEnd;
    int starIntTime = (int) startLong;
    int endIntTime = (int) endLong;


    mp = MediaPlayer.create(this, R.raw.my_sound_file);
    mp.seekTo(starIntTime);
    mp.start();

    Handler handler = new Handler();
    handler.postDelayed(stopPlayerTask, endIntTime);
}

Upvotes: 0

Juliyanage Silva
Juliyanage Silva

Reputation: 2699

Yes after initiating the MediaPlayer and loading the file , you can pause or stop the player after using specific time

while(mPlayer.isPlaying()) {
          if(mPlayer.getCurrentPosition() > 7254 && mPlayer.getCurrentPosition() < 7410 ){ 
              labelTxt.setText("Stop: " + mPlayer.getCurrentPosition() );
              mPlayer.stop();
              break;
          }
        }

Upvotes: 1

Related Questions