Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynctask Music not stopping when power button pressed
    primarykey
    data
    text
    <p>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.</p> <pre><code>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&lt;Void, Void, Void&gt; { 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload