android - android旋转后如何更新SeekBar?

标签 android seekbar android-mediaplayer

我使用 this media player但是当我旋转模拟器 (Ctrl+F11) 时,startPlayProgressUpdater() 无法正常工作。 旋转后如何更新搜索栏?

最佳答案

在 Activity 的 onSaveInstanceState() 回调中保存搜索栏位置。

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("current_position", seekbarPosition);
    }

在您的 Activity 的 onCreate

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  .......
  if (savedInstanceState != null) {
    // set seek bar position from `savedInstanceState`
  }
}

更新

将可运行的通知移出启动方法。

Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };

public void startPlayProgressUpdater() {
    seekBar.setProgress(mediaPlayer.getCurrentPosition());

    if (mediaPlayer.isPlaying()) {
        handler.postDelayed(notification,1000);
    }else{
        mediaPlayer.pause();
        buttonPlayStop.setText(getString(R.string.play_str));
        seekBar.setProgress(0);
    }
} 

然后在 onSaveInstanceState() 方法中从处理程序中删除所有回调。

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("current_position", seekbarPosition);
    handler.removeCallbacks(notification);
}

关于android - android旋转后如何更新SeekBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7459853/

相关文章:

android - MediaPlayer.seekTo() 不寻求在 Android 上定位

android - 如何在Android中关闭系统对话框?

android - android 中搜索栏运行时错误

android - 如何在vitamio中实现拖动seekbar时即时seek

java - 错误 "E/MediaPlayer: Should have subtitle controller already set"导致没有音频播放

android - MediaPlayer 没有被发布就完成了

如果在 android lollipop 中关闭蓝牙,Android ble 应用程序会崩溃

未为超链接加载的 Android webview 内容包含哈希

java - CountdownTimer 崩溃应用程序。尝试更新 UI 线程

android - 如何取消线程