Android旋转后如何更新SeekBar?

发布于 2024-12-05 07:26:27 字数 179 浏览 1 评论 0原文

我使用这个媒体播放器但是当我旋转时模拟器 (Ctrl+F11) startPlayProgressUpdater() 无法正常工作。 旋转后如何更新搜索栏?

I use this media player but when I rotate the emulator (Ctrl+F11) the startPlayProgressUpdater() doesn't work properly.
How can I update seekbar after rotation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2024-12-12 07:26:27

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

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

在 Activity

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

Update

onCreate 中,将通知可运行状态移出 start 方法。

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);
}

Save the seekbar position in onSaveInstanceState() callback in activity.

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

In onCreate of your activity

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

Update

Move the notification runnable out of the start method.

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);
    }
} 

Then in the onSaveInstanceState() method remove all callbacks from the handler.

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putInt("current_position", seekbarPosition);
    handler.removeCallbacks(notification);
}
夏雨凉 2024-12-12 07:26:27

我已经按照这种方式更改了程序,解决方案如下。

package org.example.java;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;

public class Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, Runnable  {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;

private final Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {

    Log.d("Player", "got to onCreate()");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initViews();
    if (savedInstanceState != null) {
        Log.d("Player", "got to savedInstanceState id not null");
        mediaPlayer.seekTo(savedInstanceState.getInt("current_position"));
        seekBar.setProgress(savedInstanceState.getInt("current_position"));
        handler.post(this);
    }
} 

@Override 
public void onSaveInstanceState(Bundle outState) {
    Log.d("Player", "got to onSaveInstanceState()");
    super.onSaveInstanceState(outState);
    outState.putInt("current_position", mediaPlayer.getCurrentPosition());
    handler.removeCallbacks(this);
    Log.d("Player", "got to End of onSaveInstanceState");

}

private void initViews() { 
    Log.d("Player", "got to initViews()");
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(this); 

    mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
    mediaPlayer.setOnCompletionListener(this);
    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(this);
    Log.d("player", "got to End of initViews()");
}

@Override
public void onClick(View v) {
    Log.d("Player", "got to onClick");

    switch (v.getId()) {
    case R.id.ButtonPlayStop:
        buttonClick();
        break;

    default:
        break;
    }
    Log.d("Player", "got to End of onClick");

}

private void buttonClick() {
    Log.d("Player", "got to buttonClick");
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(R.string.pause_str);
        try {
            mediaPlayer.start();
            Log.d("Player", "got to before startPlayProgressUpdater in buttonClick");
            startPlayProgressUpdater();
        } catch (IllegalStateException e) {
            mediaPlayer.pause(); 
        }
    } else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause();
        seekBar.setProgress(mediaPlayer.getCurrentPosition());
    }
    Log.d("Player", "got to End of buttonClick");

}

public void startPlayProgressUpdater() {
    Log.d("Player", "got to startPlayProgressUpdater()");
    seekBar.setProgress(mediaPlayer.getCurrentPosition());
    int pos = mediaPlayer.getCurrentPosition();
    Log.d("Player", "Current position is " + Integer.toString(pos));
    Log.d("Player", "got to After seekBar.setProgress");
    if (mediaPlayer.isPlaying()) { 
        Log.d("Player", "got to before handler.postDelayed");
        handler.postDelayed(this, 1000);
        Log.d("Player", "got to after handler.postDelayed");

    } else if (mediaPlayer.getCurrentPosition() < mediaPlayer.getDuration()) {
        mediaPlayer.pause();
        buttonPlayStop.setText(R.string.play_str);
    }
    Log.d("Player", "got to End of startPlayProgressUpdater()");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.d("Player", "got to onTouch");

    seekChange(v);
    return false;
}

private void seekChange(View v) {
    Log.d("Player", "got to seekChange");

    if (mediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mediaPlayer.seekTo(sb.getProgress());
    }
}

@Override
public void run() {
    Log.d("Player", "got to run()");

    startPlayProgressUpdater();
    Log.d("Player", "got to after startPlayProgressUpdater in run()");

}

@Override
public void onCompletion(MediaPlayer mp) {
    mp.seekTo(0);
}
}

更新

毕竟我找到了解决方案,但它很脏:D

package org.example.java;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;

public class Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, Runnable  {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;

private final Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {

    Log.d("Player", "got to onCreate()");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initViews();
    if (savedInstanceState != null) {
        Log.d("Player", "got to savedInstanceState id not null");
        mediaPlayer.seekTo(savedInstanceState.getInt("current_position"));
        seekBar.setProgress(savedInstanceState.getInt("current_position"));
        mediaPlayer.start();
        handler.post(this);
    }
} 

@Override 
public void onSaveInstanceState(Bundle outState) {
    Log.d("Player", "got to onSaveInstanceState()");
    super.onSaveInstanceState(outState); 
    outState.putInt("current_position", mediaPlayer.getCurrentPosition());
    handler.removeCallbacks(this);
    Log.d("Player", "got to End of onSaveInstanceState");
    pauseMedia();

}

private void initViews() { 
    Log.d("Player", "got to initViews()");
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(this); 

    mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
    mediaPlayer.setOnCompletionListener(this);
    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(this);
    Log.d("player", "got to End of initViews()");
}

@Override
public void onClick(View v) {
    Log.d("Player", "got to onClick");

    switch (v.getId()) {
    case R.id.ButtonPlayStop:
        buttonClick();
        break;

    default:
        break;
    }
    Log.d("Player", "got to End of onClick");

}

private void buttonClick() {
    Log.d("Player", "got to buttonClick");
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(R.string.pause_str);
        try {
            mediaPlayer.start();
            Log.d("Player", "got to before startPlayProgressUpdater in buttonClick");
            startPlayProgressUpdater();
        } catch (IllegalStateException e) {
            mediaPlayer.pause(); 
        }
    } else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause();
        seekBar.setProgress(mediaPlayer.getCurrentPosition());
    }
    Log.d("Player", "got to End of buttonClick");

}

public void startPlayProgressUpdater() {
    Log.d("Player", "got to startPlayProgressUpdater()");
    seekBar.setProgress(mediaPlayer.getCurrentPosition());
    int pos = mediaPlayer.getCurrentPosition();
    Log.d("Player", "Current position is " + Integer.toString(pos));
    Log.d("Player", "got to After seekBar.setProgress");
    if (mediaPlayer.isPlaying()) { 
        Log.d("Player", "got to before handler.postDelayed");
        handler.postDelayed(this, 1000);
        Log.d("Player", "got to after handler.postDelayed");

    } else if (mediaPlayer.getCurrentPosition() < mediaPlayer.getDuration()) {
        mediaPlayer.pause();
        buttonPlayStop.setText(R.string.play_str);
    }
    Log.d("Player", "got to End of startPlayProgressUpdater()");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.d("Player", "got to onTouch");

    seekChange(v);
    return false;
}

private void seekChange(View v) {
    Log.d("Player", "got to seekChange");

    if (mediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mediaPlayer.seekTo(sb.getProgress());
    }
}

@Override
public void run() {
    Log.d("Player", "got to run()");

    startPlayProgressUpdater();
    Log.d("Player", "got to after startPlayProgressUpdater in run()");

}

@Override
public void onCompletion(MediaPlayer mp) {
    mp.seekTo(0);
}

public void pauseMedia() {
    mediaPlayer.pause();
}

}

更新2:

我找到了另一种方法:
使用 getLastNonConfigurationInstance() 方法。

I have changed the program in this way, solution below.

package org.example.java;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;

public class Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, Runnable  {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;

private final Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {

    Log.d("Player", "got to onCreate()");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initViews();
    if (savedInstanceState != null) {
        Log.d("Player", "got to savedInstanceState id not null");
        mediaPlayer.seekTo(savedInstanceState.getInt("current_position"));
        seekBar.setProgress(savedInstanceState.getInt("current_position"));
        handler.post(this);
    }
} 

@Override 
public void onSaveInstanceState(Bundle outState) {
    Log.d("Player", "got to onSaveInstanceState()");
    super.onSaveInstanceState(outState);
    outState.putInt("current_position", mediaPlayer.getCurrentPosition());
    handler.removeCallbacks(this);
    Log.d("Player", "got to End of onSaveInstanceState");

}

private void initViews() { 
    Log.d("Player", "got to initViews()");
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(this); 

    mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
    mediaPlayer.setOnCompletionListener(this);
    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(this);
    Log.d("player", "got to End of initViews()");
}

@Override
public void onClick(View v) {
    Log.d("Player", "got to onClick");

    switch (v.getId()) {
    case R.id.ButtonPlayStop:
        buttonClick();
        break;

    default:
        break;
    }
    Log.d("Player", "got to End of onClick");

}

private void buttonClick() {
    Log.d("Player", "got to buttonClick");
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(R.string.pause_str);
        try {
            mediaPlayer.start();
            Log.d("Player", "got to before startPlayProgressUpdater in buttonClick");
            startPlayProgressUpdater();
        } catch (IllegalStateException e) {
            mediaPlayer.pause(); 
        }
    } else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause();
        seekBar.setProgress(mediaPlayer.getCurrentPosition());
    }
    Log.d("Player", "got to End of buttonClick");

}

public void startPlayProgressUpdater() {
    Log.d("Player", "got to startPlayProgressUpdater()");
    seekBar.setProgress(mediaPlayer.getCurrentPosition());
    int pos = mediaPlayer.getCurrentPosition();
    Log.d("Player", "Current position is " + Integer.toString(pos));
    Log.d("Player", "got to After seekBar.setProgress");
    if (mediaPlayer.isPlaying()) { 
        Log.d("Player", "got to before handler.postDelayed");
        handler.postDelayed(this, 1000);
        Log.d("Player", "got to after handler.postDelayed");

    } else if (mediaPlayer.getCurrentPosition() < mediaPlayer.getDuration()) {
        mediaPlayer.pause();
        buttonPlayStop.setText(R.string.play_str);
    }
    Log.d("Player", "got to End of startPlayProgressUpdater()");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.d("Player", "got to onTouch");

    seekChange(v);
    return false;
}

private void seekChange(View v) {
    Log.d("Player", "got to seekChange");

    if (mediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mediaPlayer.seekTo(sb.getProgress());
    }
}

@Override
public void run() {
    Log.d("Player", "got to run()");

    startPlayProgressUpdater();
    Log.d("Player", "got to after startPlayProgressUpdater in run()");

}

@Override
public void onCompletion(MediaPlayer mp) {
    mp.seekTo(0);
}
}

Update

After all I find the solution, but it is very dirty :D

package org.example.java;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;

public class Player extends Activity implements OnClickListener, OnTouchListener, OnCompletionListener, Runnable  {

private Button buttonPlayStop;
private MediaPlayer mediaPlayer;
private SeekBar seekBar;

private final Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {

    Log.d("Player", "got to onCreate()");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    initViews();
    if (savedInstanceState != null) {
        Log.d("Player", "got to savedInstanceState id not null");
        mediaPlayer.seekTo(savedInstanceState.getInt("current_position"));
        seekBar.setProgress(savedInstanceState.getInt("current_position"));
        mediaPlayer.start();
        handler.post(this);
    }
} 

@Override 
public void onSaveInstanceState(Bundle outState) {
    Log.d("Player", "got to onSaveInstanceState()");
    super.onSaveInstanceState(outState); 
    outState.putInt("current_position", mediaPlayer.getCurrentPosition());
    handler.removeCallbacks(this);
    Log.d("Player", "got to End of onSaveInstanceState");
    pauseMedia();

}

private void initViews() { 
    Log.d("Player", "got to initViews()");
    buttonPlayStop = (Button) findViewById(R.id.ButtonPlayStop);
    buttonPlayStop.setOnClickListener(this); 

    mediaPlayer = MediaPlayer.create(this, R.raw.testsong_20_sec);
    mediaPlayer.setOnCompletionListener(this);
    seekBar = (SeekBar) findViewById(R.id.SeekBar01);
    seekBar.setMax(mediaPlayer.getDuration());
    seekBar.setOnTouchListener(this);
    Log.d("player", "got to End of initViews()");
}

@Override
public void onClick(View v) {
    Log.d("Player", "got to onClick");

    switch (v.getId()) {
    case R.id.ButtonPlayStop:
        buttonClick();
        break;

    default:
        break;
    }
    Log.d("Player", "got to End of onClick");

}

private void buttonClick() {
    Log.d("Player", "got to buttonClick");
    if (buttonPlayStop.getText() == getString(R.string.play_str)) {
        buttonPlayStop.setText(R.string.pause_str);
        try {
            mediaPlayer.start();
            Log.d("Player", "got to before startPlayProgressUpdater in buttonClick");
            startPlayProgressUpdater();
        } catch (IllegalStateException e) {
            mediaPlayer.pause(); 
        }
    } else {
        buttonPlayStop.setText(getString(R.string.play_str));
        mediaPlayer.pause();
        seekBar.setProgress(mediaPlayer.getCurrentPosition());
    }
    Log.d("Player", "got to End of buttonClick");

}

public void startPlayProgressUpdater() {
    Log.d("Player", "got to startPlayProgressUpdater()");
    seekBar.setProgress(mediaPlayer.getCurrentPosition());
    int pos = mediaPlayer.getCurrentPosition();
    Log.d("Player", "Current position is " + Integer.toString(pos));
    Log.d("Player", "got to After seekBar.setProgress");
    if (mediaPlayer.isPlaying()) { 
        Log.d("Player", "got to before handler.postDelayed");
        handler.postDelayed(this, 1000);
        Log.d("Player", "got to after handler.postDelayed");

    } else if (mediaPlayer.getCurrentPosition() < mediaPlayer.getDuration()) {
        mediaPlayer.pause();
        buttonPlayStop.setText(R.string.play_str);
    }
    Log.d("Player", "got to End of startPlayProgressUpdater()");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    Log.d("Player", "got to onTouch");

    seekChange(v);
    return false;
}

private void seekChange(View v) {
    Log.d("Player", "got to seekChange");

    if (mediaPlayer.isPlaying()) {
        SeekBar sb = (SeekBar) v;
        mediaPlayer.seekTo(sb.getProgress());
    }
}

@Override
public void run() {
    Log.d("Player", "got to run()");

    startPlayProgressUpdater();
    Log.d("Player", "got to after startPlayProgressUpdater in run()");

}

@Override
public void onCompletion(MediaPlayer mp) {
    mp.seekTo(0);
}

public void pauseMedia() {
    mediaPlayer.pause();
}

}

Update 2:

I have find another way:
using getLastNonConfigurationInstance() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文