1

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

4

2 回答 2

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 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);
}
于 2011-09-18T07:11:42.817 回答
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:

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

于 2011-09-19T10:01:27.943 回答