У меня есть приложение для музыкального плеера, и у меня есть проблема. Когда я вызываю BackPressed или заканчивает(), он переходит к методу 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
попробуйте использовать musicplayer.release() onBackpressed метод перед musicplayer.stop(); –