我希望在应用程序最小化时暂停 MediaPlayer 音乐,并在恢复时从同一点恢复,但我不能这样做,每次从一开始就开始......
这是我所拥有的:
public class Main extends Activity {
MediaPlayer music;
int length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
music = MediaPlayer.create(Main.this, R.raw.song);
music.setLooping(true);
music.start();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
music.pause();
length = music.getCurrentPosition();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
music.start();
music.seekTo(length);
}
}