我想显示歌曲的持续时间。
public class Songs extends AppCompatActivity {
MediaPlayer song;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surah);
song=MediaPlayer.create(Songs.this,R.raw.song);
}
public void play1(View v){
song.start();
}
public void pause1(View v){
song.pause();
}
}
答案 0 :(得分:0)
您可以使用Timer
,它将每1000毫秒更新一次。然后将时间设置为TextView
或您想要的时间。
song.start();
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (song != null && song.isPlaying()) {
tv.post(new Runnable() {
@Override
public void run() {
tv.setText(song.getCurrentPosition());
}
});
} else {
timer.cancel();
timer.purge();
}
}
});
}
}, 0, 1000);