0
我有一個音樂播放器應用程序,並且出現問題。當我調用BackPressed或finish()時,它跳轉到onPause方法不完成()。爲什麼?我無法完成我的應用程序。非常感謝您的幫助。關閉應用程序時出現異常
我的代碼
public class MusicPlayer extends ThemableActivity implements SeekBar.OnSeekBarChangeListener {
MediaPlayer musicPlayer;
int abc;
@Override
protected void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.media_player_layout);
Intent intent = getIntent();
String track = intent.getDataString();
TextView txt = (TextView)findViewById(R.id.txt);
txt.setGravity(Gravity.CENTER);
txt.setText(track);
ImageView img = (ImageView)findViewById(R.id.img);
img.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
final Uri uri = getIntent() != null ? getIntent().getData() : null;
final SharedPreferences premiumSettings = getSharedPreferences("PREMIUM", Context.MODE_PRIVATE);
final boolean isPremium = premiumSettings.getBoolean("isPremium", false);
final SeekBar seekBar = (SeekBar) findViewById(R.id.seekBar);
TextView max = (TextView)findViewById(R.id.max);
final TextView actual = (TextView)findViewById(R.id.actual);
seekBar.setOnSeekBarChangeListener(this);
Button loop = (Button)findViewById(R.id.loop);
if (isPremium) {
musicPlayer = MediaPlayer.create(MusicPlayer.this, uri);
musicPlayer.setWakeMode(this.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
seekBar.setMax(musicPlayer.getDuration());
musicPlayer.start();
max.setText(String.format("%dm %ds",
TimeUnit.MILLISECONDS.toMinutes((long) musicPlayer.getDuration()),
TimeUnit.MILLISECONDS.toSeconds((long) musicPlayer.getDuration()) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) musicPlayer.getDuration()))));
final ImageButton play = (ImageButton) findViewById(R.id.play);
if(!musicPlayer.isPlaying()){
play.setImageResource(android.R.drawable.ic_media_play);
}
else{
play.setImageResource(android.R.drawable.ic_media_pause);
}
play.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
if(musicPlayer.isPlaying()){
play.setImageResource(android.R.drawable.ic_media_play);
musicPlayer.stop();
return true;
}
return false;
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = musicPlayer.getCurrentPosition();
if(musicPlayer.isPlaying()){
musicPlayer.pause();
play.setImageResource(android.R.drawable.ic_media_play);
}
else if(!musicPlayer.isPlaying()) {
musicPlayer.seekTo(position);
musicPlayer.start();
play.setImageResource(android.R.drawable.ic_media_pause);
}}
});
loop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
musicPlayer.setLooping(true);
}
});
final Handler mHandler = new Handler();
Runnable mRunnable;
mRunnable = new Runnable() {
@Override
public void run() {
int mCurrentPosition = 0;
if (musicPlayer != null) {
mCurrentPosition = musicPlayer.getCurrentPosition();
seekBar.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
actual.setText(String.format("%dm %ds",
TimeUnit.MILLISECONDS.toMinutes((long) mCurrentPosition),
TimeUnit.MILLISECONDS.toSeconds((long) mCurrentPosition) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) mCurrentPosition))));
}
};
mRunnable.run();
ImageButton exit = (ImageButton) findViewById(R.id.exit);
exit.setImageResource(android.R.drawable.btn_dialog);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(musicPlayer.isPlaying()) {
musicPlayer.setLooping(false);
musicPlayer.stop();
}
musicPlayer = null;
finish();
}
});
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayer.this);
builder.setTitle("Error");
builder.setMessage("You are not premium user. Please enter the promocode or buy the full version");
builder.setIcon(R.drawable.holo_dark_action_info);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}}
public void onBackPressed() {
if(musicPlayer.isPlaying()) {
musicPlayer.setLooping(false);
musicPlayer.stop();
}
musicPlayer = null;
finish();
}
@Override
public void onPause(){
super.onPause();
abc = musicPlayer.getCurrentPosition();
musicPlayer.pause();
}
@Override
public void onResume(){
super.onResume();
musicPlayer.seekTo(abc);
musicPlayer.start();
}
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
if (musicPlayer != null && b){
musicPlayer.seekTo(i);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
好了,什麼是例外?什麼是logcat堆棧跟蹤? – EpicPandaForce 2014-10-07 15:31:58
嘗試在musicplayer.stop()之前使用musicplayer.release()onBackpressed方法; – 2014-10-07 15:41:52