我的活动中有一个按钮。单击我正在呼叫的按钮finish()
,问题是我正在Illegal state exception
接听电话以完成。请帮忙。
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import com.mds.perfumastic.R;
import com.mds.perfumastic.constants.Constants;
public class AudioPlayerActivity extends Activity implements
OnPreparedListener , MediaPlayerControl{
private Handler handler = new Handler();
private final MediaPlayer mediaPlayer = new MediaPlayer();
private MediaController mediaController;
private ProgressDialog progressBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_audioplayer);
progressBar = new ProgressDialog(this);
progressBar.setMessage("Playing...");
progressBar.show();
String audioUrl = getIntent().getStringExtra(Constants.INTENT_EXTRA_AUDIO_URL);
mediaController = new MediaController(this);
try {
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.reset();
mediaPlayer.setDataSource(audioUrl);
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onPause() {
super.onPause();
progressBar.dismiss();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
// --MediaPlayerControl
// methods----------------------------------------------------
public void start() {
mediaPlayer.start();
}
public void pause() {
mediaPlayer.pause();
}
public int getDuration() {
return mediaPlayer.getDuration();
}
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int i) {
mediaPlayer.seekTo(i);
}
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
public int getBufferPercentage() {
return 0;
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}
// --------------------------------------------------------------------------------
public void onPrepared(final MediaPlayer mediaPlayer) {
Log.d("TAG", "onPrepared");
progressBar.hide();
mediaController.setMediaPlayer(this);
mediaController.setAnchorView(findViewById(R.id.newView));
mediaPlayer.start();
handler.post(new Runnable() {
public void run() {
mediaController.setEnabled(true);
mediaController.show(mediaPlayer.getDuration());
}
});
}
/** Use screen touches to toggle the video between playing and paused. */
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
mediaPlayer.start();
mediaController.show(mediaPlayer.getDuration());
}
return true;
} else {
return false;
}
}
public void onClick(View v) {
if(v.getId() == R.id.btn_back) {
finish();
}
}
}
这是日志消息:
12-12 11:55:34.791: E/AndroidRuntime(1336): FATAL EXCEPTION: main
12-12 11:55:34.791: E/AndroidRuntime(1336): java.lang.IllegalStateException
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.media.MediaPlayer.getCurrentPosition(Native Method)
12-12 11:55:34.791: E/AndroidRuntime(1336): at com.mds.perfumastic.activites.AudioPlayerActivity.getCurrentPosition(AudioPlayerActivity.java:80)
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.widget.MediaController.setProgress(MediaController.java:415)
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.widget.MediaController.access$500(MediaController.java:71)
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.widget.MediaController$3.handleMessage(MediaController.java:386)
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.os.Looper.loop(Looper.java:137)
12-12 11:55:34.791: E/AndroidRuntime(1336): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-12 11:55:34.791: E/AndroidRuntime(1336): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 11:55:34.791: E/AndroidRuntime(1336): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 11:55:34.791: E/AndroidRuntime(1336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-12 11:55:34.791: E/AndroidRuntime(1336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-12 11:55:34.791: E/AndroidRuntime(1336): at dalvik.system.NativeStart.main(Native Method)