2

In my application background music is controlled by an asynctask. When the home button is pressed the music shuts off. When the power button is pressed it just continues playing. I also realized if i press the power button then resume it it starts to copies of the song at the same time.

private single s;
private BackgroundSound mBackgroundSound = new BackgroundSound();


protected void onPause(){
    super.onPause();
    Log.d("mSTATE","PAUSE");
    s.onPause();
    mBackgroundSound.cancel(true);
}

public void onBackPressed() {
       super.onBackPressed();
       return;
}

protected void onDestroy(){
    super.onDestroy();
    Log.d("mState","DESTROY");
}

public void onWindowFocusChanged(boolean change){
    super.onWindowFocusChanged(change);

}

@Override
protected void onResume(){
    super.onResume();
    mBackgroundSound = new BackgroundSound();
    mBackgroundSound.execute(null);
    Log.d("mSTATE","RESUME");
    s.onResume();

}

@Override
protected void onStop(){
    super.onStop();
    Log.d("mSTATE","STOP");
    s.freeMemory();
    mBackgroundSound.end();


}

@Override
protected void onRestart(){
    super.onRestart();
    Log.d("mSTATE","RESTART");
}

@Override 
protected void onStart(){
    super.onStart();
    Log.d("mSTATE","STARTER");
}


public class BackgroundSound extends AsyncTask<Void, Void, Void> {
    private MediaPlayer player;
    protected void onPreExecute() {
        player = MediaPlayer.create(SingleActivity.this, R.raw.song);
    }
    @Override
    protected Void doInBackground(Void... params) {

        player.setLooping(true);
        AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
         int currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
         int maxRingerVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
         double proportion = currentVolume/(double)maxRingerVolume;
         int maxMusicVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
         int volume = (int)(proportion * maxMusicVolume);
        player.setVolume(volume,volume); 
        player.setLooping(true);
        player.start(); 
        return null;
    }

    public void end(){
        player.stop();
        player.release();
    }

}
MikeC
  • 725
  • 8
  • 19

3 Answers3

2

AFAIK when Power button is pressed, only onpause is called and no onstop is called, Since no other activity has come to the foreground, Only a system window has come on top. Hence your end call is not being called.

A better way would be to override Asynctask.onCancelled and call Asynctask.cancel method in OnPause rather than onstop

nandeesh
  • 24,740
  • 6
  • 69
  • 79
1

Just to add, you need to override onCancelled() and add check in doInBackground(). See: Ideal way to cancel an executing AsyncTask

Community
  • 1
  • 1
nandaka
  • 101
  • 1
  • 6
  • what code should i add whe noverriding onCancelled? Should it be everything thats in the end()? and what exactly is checking in doinbackgroudn()? Thanks for your help – MikeC Sep 05 '12 at 00:30
0

You have to cancel your asyncTask. You should call asyncTask.cancel(true) in your Activity's onDestroy().

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103