Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8473543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:22:47+00:00 2026-06-10T17:22:47+00:00

In my application background music is controlled by an asynctask. When the home button

  • 0

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();
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T17:22:49+00:00Added an answer on June 10, 2026 at 5:22 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have background music playing in my application. in: -(void)viewDidLoad I start the
My application has some background music playing on a loop, using a MediaPlayer .
I want to send my Application to background simply clicking on Button inside my
I want to play background music in my game application using AVAudio player.I am
I am using a service to run a background music for the whole application.
I'm building an application with background music. I've written a class to play the
I my application I have a background music that plays when the program starts.
In My application,I am playing a music using the following code.But i want to
As My application supports background task I have an Exit button which will call
Application has 7-8 activities, so I have create an application with some background music

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.