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 6748505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:34:23+00:00 2026-05-26T12:34:23+00:00

Okey, this is my problem. I have one service class where Ive managed to

  • 0

Okey, this is my problem. I have one service class where Ive managed to create media player to play music in background all time. Here is code:

package com.test.brzoracunanje;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {

    return null;
}
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
       player = MediaPlayer.create(this, R.raw.test_cbr);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);
        player.start();
}
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
}
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

protected void onNewIntent() {
    player.pause();
}
}

But now I have problem when I click on HOME, or BACK button. It still plays music. Does anyone knows how to solve that problem?

And here is code how i call this service on class where I want to play music;

  Intent svc=new Intent(this, BackgroundSoundService.class);
    startService(svc);
  • 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-05-26T12:34:24+00:00Added an answer on May 26, 2026 at 12:34 pm

    If you want to play background music for your app only, then play it in a thread launched from your app/use AsyncTask class to do it for you.

    The concept of services is to run in the background; By background, the meaning is usually when your app UI is NOT VISIBLE. True, it can be used just like you have (If you remember to stop it) but its just not right, and it consumes resources you shouldn’t be using.

    If you want to peform tasks on the background of your activity, use AsyncTask.

    By the way, onStart is deprecated. When you do use services, implement onStartCommand.

    UPDATE:

    I think this code will work for you. Add this class (Enclosed in your activity class).

    public class BackgroundSound extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected Void doInBackground(Void... params) {
            MediaPlayer player = MediaPlayer.create(YourActivity.this, R.raw.test_cbr); 
            player.setLooping(true); // Set looping 
            player.setVolume(1.0f, 1.0f); 
            player.start(); 
    
            return null;
        }
    
    }
    

    Now, in order to control the music, save your BackgroundSound object instead of creating it annonymously. Declare it as a field in your activity:

    BackgroundSound mBackgroundSound = new BackgroundSound();
    

    On your activity’s onResume method, start it:

    public void onResume() {
        super.onResume();
        mBackgroundSound.execute(null);
    }
    

    And on your activity’s onPause method, stop it:

    public void onPause() {
        super.onPause();
        mBackgroundSound.cancel(true);
    }
    

    This will work.

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

Sidebar

Related Questions

Okey, so this is my problem. I have a page structure saved into a
Okey I have a class that prints the board and another class that prints
Okey, so this is my problem. I want to use mod_rewrite to make nice
Okey this is really strange. I have this script which basically downloads bunch of
I have a problem posting file via ajax jQuery function. I have something like
Okey, here it goes. I have a block of code that repeats it self
Lets say i have a Shape object that has a constructor like this: Shape(
I have 2 fields that can be use in row area. One of them
I have the following test-class made on the fly: http://sumoin.pastebin.com/ff744ea4 No fine-tuning or something
I found the problem with PopulateOnDemand with my treview class derived from System.Web.UI TreeView.

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.