Stopping background service with change of activity in android - android

I have created a background service in android in java which plays music at the start of activity.
package com.sudarshan.tictactoenew;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.app.Service;
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.c);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return startId;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
//calling background service
Intent intent = new Intent(MenuPage.this, BackgroundSoundService.class);
startService(intent);
But this runs forever, I mean till the app is not closed.How do I stop this when I move to the next activity?I am calling background service in menupage activity

Related

Service background music does not turn off

In my app there is a background music, for what I have a service. It works fine, but it "keeps playing" when either I close the app or turn off the screen.
Should I change something in onPause or onStop?
My code:
public class MyService extends Service {
private final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.id);
player.setLooping(true); // Set looping
player.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
protected void onStop() {
player.pause();
}
public void onPause() {
player.pause();
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
As screen turns off your activity becomes invisible which triggers onPause followed by onStop
Screen on, on the other hand, triggers onStart followed by onResume.
when ever you want to stop service use
stopService(new Intent(ActivityName.this, ServiceClassName.class));
Probably copies of the player are created somehow. Try to make MediaPlayer variable static and then try to deallocate it after pausing onPause(). Anyway it is weird, that you are creating MediaPlayer instance in service.

Background music doesn't turn off or on when it is supposed to

I've got a service which plays background music in all of my activities, but doesn't turn off while onPause or onStop. I want my music to turn off while apps close or screen turn off, doesnt matter in which activity usesr currently is.
The problem is this:
I have a service. In every activity I coded what to do onPause and onStop. It works fine, but service also stops when I switch to another activity.
Code:
public class MyService extends Service {
private final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.id);
player.setLooping(true); // Set looping
player.setVolume(100, 100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
protected void onStop() {
player.pause();
}
public void onPause() {
player.pause();
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
You will have to stop the service in the onDestroy method of your main activity. Check this answer

My background music service in Android app keeps on playing even when I close the app

I'm using a background service to run a background music for my app in all of it's activities! The issue is that when the app run, it works fine but when I close it it keeps on playing the music until I uninstall it from the device!
What do you think the problem here?
Here is The code In my Background service:
/**
* Created by Naira on 12/5/2016.
*/
public class Background_music extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.music);
player.setLooping(true); // Set looping
player.setVolume(50,50);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return START_STICKY;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
player.stop();
player.release();
player = null;
}
#Override
public void onLowMemory() {
}
}
An here the code in my first activity to run it as an Intent:
Intent svc=new Intent(this, Background_music.class);
startService(svc);
And, of course I did declare it in my manifest =)
Thanks in advance!
In the onDestroy() method of your MainActivity you have to stop the service.
#Override
protected void onDestroy() {
super.onDestroy();
if(isMyServiceRunning(Background_music.class))
{
stopService(new Intent(this, Background_music.class));
}
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Hope this helps you.

Loading sound once for all the activity

Iam trying to use soundpool to play sound in my android app. I have 3 activities where i have to play sound. So can i load sounds globally once only when application starts?? Can i make a global class and load all the sound once and use them in other activities.
You can try this example
//start service
Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);
//Add in manifest file
<service
android:enabled="true"
android:name="com.package.name.BackgroundSoundService" />
public class BackgroundSoundService extends Service {
private static final String TAG = null;
MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.idil);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TO DO
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
Yes you can load your audio in a global class. Thankfully android is providing us such class called Application.java.
public class TestApp extends Application {
private static final String TAG = "TestApp";
private static TestApp sTestApp;
private MediaPlayer mMediaPlayer;
#Override
public void onCreate() {
super.onCreate();
sTestApp = this;
mMediaPlayer = MediaPlayer.create(this, R.raw.music);
mMediaPlayer.setVolume(1, 1); //Volume should have to be between 0.0 to 1.0
}
public static TestApp getInstance() {
return sTestApp;
}
public void play() {
mMediaPlayer.start();
}
}
Then declare this as application class in your manifest
<application
android:name=".TestApp"
android:label="#string/app_name"
android:theme="#style/AppTheme">
.........
</application>
Now this class will be started automatically when your application starts and will be destroyed after your application completely destroys.
Now you can play the audio by calling
TestApp.getInstance().play();
from wherever you want.

Controlling Background Music android

I am using a Service to play background Music. The problem is that the music continues playing when i have finished the activity. when i press home button the music stop i want to make music continue playing while application is use and stop when home button press help me please. can some tell me how to do this
private static final String TAG = null;
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
player = MediaPlayer.create(this, R.raw.sample);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TODO
}
public IBinder onUnBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onStop() {
}
public void onPause() {
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
You should use a service and play the music.
http://developer.android.com/guide/components/services.html
Service runs in the background so even when you switch activities it still runs and you can play your music there.
You can start a service and then stop it when you need. Also you can bind the service to the activity. Look at the docs in the above link for more info.
Example :
https://thenewcircle.com/s/post/60/servicesdemo_using_android_services
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
private static final String TAG = "MyService";
MediaPlayer player;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
player = MediaPlayer.create(this, R.raw.braincandy);
// song is res/raw/yourmusicfile.mp3
player.setLooping(false); // Set looping
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
player.stop();
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
player.start();
}
}
Now just start the service and you can stop the service when you need. You can switch between activities and the service still runs playing the music.
Since the code i huge i uploaded to DropBox. Here's the sample i made
https://www.dropbox.com/s/fjc4krpcwsg7qm6/ServicesDemo.zip

Categories

Resources