Android Background Music

Okey, this is my problem. I have one service class where Ive managed to create a media player to play music in the background all the time. Here is the 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 a problem when I press the HOME or BACK button. He still plays music. Does anyone know how to solve this problem?

And here is the code, as I call this service in the classroom, where I want to play music;

  Intent svc=new Intent(this, BackgroundSoundService.class); startService(svc); 
+7
source share
7 answers

If you want to play only background music only for your application, then play it in the stream launched from your application / use the AsyncTask class to do this for you.

The concept of services is to run in the background; At its discretion, the value usually occurs when the user interface of the application is NOT visible . True, it can be used in the same way as yours (if you stop it), but it is simply not correct, and it consumes resources that you should not use.

If you want to perform tasks against a background of activity , use AsyncTask.

By the way, onStart deprecated. When you use services, run onStartCommand .

UPDATE:

I think this code will work for you. Add this class (included 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, to control the music, save the BackgroundSound object, rather than create it anonymously. Declare it as a field in your activity:

 BackgroundSound mBackgroundSound = new BackgroundSound(); 

In your activity onResume method, run it:

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

And in your activity onPause method, stop it:

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

This will work.

+22
source

AsyncTasks are only good for very short clips, but if it is a music file, you will encounter problems such as:

  • You cannot stop / pause music in the middle. This is a real problem for me.

Here are a few lines on the Android Developers page about AsyncTasks

AsyncTasks is ideal for short operations (a few seconds in most cases.) If you need threads to run for long periods of time, it is highly recommended that you use the various APIs provided by the java.util.concurrent package, such as Executor, ThreadPoolExecutor and FutureTask.

So, I'm currently considering other options and updating the answer as soon as I find a solution.

+1
source

Of course, your service runs in the background, and you must stop it manually from your activity when it goes to the background.

0
source

yes, u should stop the service when u press the home button. In the override method OnPause () {} u should stop the music service. And inside the service in the OnDestroy () method u stop the media player. So, now pressing home calls onPause and onPause () β†’ stops the service, and OnDestroy is executed in the service, and in odestroy () the media player is stopped and the ur problem is fixed.

0
source

write the code below in the on stop method:

System. Output (0);

this will stop your media player when you press the back / home button or close the application

0
source

Try stopping background music in HOME or BACK.

 @Override protected void onStop() { super.onStop(); ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager .getRunningTasks(Integer.MAX_VALUE); boolean isActivityFound = false; if (services.get(0).topActivity.getPackageName().toString() .equalsIgnoreCase(getPackageName().toString())) { isActivityFound = true; // Activity belongs to your app is in foreground. } if (!isActivityFound) { if (player != null && player.isPlaying()) { player.release(); } } } 
0
source

Try inserting the player.stop () method into the onDestroy () method. This should help.

0
source

All Articles