android旋转后如何更新SeekBar?

时间:2011-09-18 06:24:17

标签: android seekbar android-mediaplayer

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

2 个答案:

答案 0 :(得分:1)

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

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

您活动的onCreate

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

更新

将通知runnable移出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);
}

答案 1 :(得分:0)

我已经用这种方式改变了程序,解决方案如下。

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:

我找到另一种方式: using getLastNonConfigurationInstance()方法。