问题
I want to set the time duration for MediaPlayer
. Here is my code:
MediaPlayer play = MediaPlayer.create(getApplicationContext(),R.raw.beepsound);
play.start();
回答1:
You can use Timer
which will be updated every 1000 milliseconds. Then set time to a TextView
or what ever you want.
if (player != null) {
player.start();
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (player != null && player.isPlaying()) {
tv.post(new Runnable() {
@Override
public void run() {
tv.setText(player.getCurrentPosition());
}
});
} else {
timer.cancel();
timer.purge();
}
}
});
}
}, 0, 1000);
}
来源:https://stackoverflow.com/questions/16052851/seting-the-duration-of-media-player-media-in-android