How to make service (background music) to turn off when I close the app or turn off the screen, but not when I switch to another activity?
Currently Music plays fine, I lasts until the screen turn off or I close the app, but also close when I switch to another activity. It is probably caused by onStop and onPause assigned to every activity. Please help me solve this issue
My service:
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.music;
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() {
}
}
My example activity has this:
public void onPause() {
super.onPause();
stopService(new Intent(a.this, MyService.class));
}
public void onStop() {
super.onStop();
stopService(new Intent(a.this, MyService.class));
}
public void onResume() {
super.onResume();
startService(new Intent(a.this, MyService.class));
}
public void onRestart() {
super.onRestart();
startService(new Intent(a.this, MyService.class));
}
There are two ways i can suggest
1) let it be in onPause(), create a flag/boolean that will set to false by default and will be true in every-case such as new activity startup, on back press etc. so if onPause is called and flag is false you can stop the music.
2) you have background service already, you can keep checking which activity is in foreground, if is homeScreen you can stop the music.
Edit :- to know if your app is not in the foreground
check this link
this is good example
Use this code.
Remove this line:
player.pause();
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.music;
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() {
}
}
Related
I'm using the BackGroundSound service music, but when I press the homebutton or power button or exiting the app the music just doesn't stop, and I need it to stop, Anyone have any suggestions/ideas how to fix it? I used this guide https://www.simplifiedcoding.net/android-service-example/
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.backgroundmusic01);
player.setLooping(true); // Set looping
player.setVolume(100,100);
}
#SuppressLint("WrongConstant")
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() {
player.stop();
}
public void onPause()
{
player.pause();
}
#Override
public void onDestroy() {
player.stop();
player.release();
}
#Override
public void onLowMemory() {
}
}
Try overriding onPause like this.
#Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null){
mediaPlayser.stop();
if (isFinishing()){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
I'm trying to play an mp3 when app is closed. It works when I'm out of the app but when i swipe it off from task manager the song ends. How can I make the service continue playing after close?
This is my service:
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onTaskRemoved(Intent rootIntent) {
Log.d("MusicService", "onTaskRemoved");
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
This is my main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button)findViewById(R.id.start);
stop = (Button)findViewById(R.id.stop);
mp = MediaPlayer.create(getApplicationContext(),R.raw.gucci);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startService(new Intent(MainActivity.this,MyService.class));
mp.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.pause();
stopService(new Intent(MainActivity.this,MyService.class));
}
});
}
NOTE: Other way may be not usable (ignore if not usable).
Don't give user the option to clear it from recents menu.
Use this manifest attribute for each activity component in your Player app
android:excludeFromRecents
just return START_STICKY in onstartcommand method of your service class . it will run forever in background .
I created a game.I added some background music to the app and now I want to pause the music when the app is exited using home button and the song should resume when it is opened from the multitasking.
package tssll8.coloursmash;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
Import android.os.IBinder;
/**
* Created by ciddarth on 04/07/17.
*/
public class bgmusic 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.bg);
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() {
}
}
Mediaplayer has a pause() method: MediaPlayer#pause()
so you could call that in the on pause which gets triggered by pressing the home button, like so
public void onPause() {
player.pause();
}
and resume it in onResume like this:
public void onResume() {
player.play();
}
I have a service, which plays background music in all of my app's activities.
In every activity I added:
public void onPause() {
super.onPause();
stopService(new Intent(OpenerPlay.this, MusicDoctorService.class));
}
public void onStop() {
super.onStop();
stopService(new Intent(OpenerPlay.this, MusicDoctorService.class));
}
public void onResume() {
super.onResume();
startService(new Intent(OpenerPlay.this, MusicDoctorService.class));
}
public void onRestart() {
super.onRestart();
startService(new Intent(OpenerPlay.this, MusicDoctorService.class));
}
My purpose was to stop or resume service which plays music when I close the app or turn off the screen. But It also stops when I switch to another activity.
Should I change something in my service's code?
Finally: I would like my service playing music to stop on screen turn off or app closing but not during activities switching.
Service:
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.music);
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() {
}
}
Try this in your Activity's onDestroy Method...
#Override
protected void onDestroy() {
super.onDestroy();
if(isMyServiceRunning(MyService.class))
{
stopService(new Intent(this, MyService.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;
}
Here when the Activity is finished you should stop the service..
Also in your service you should release the mediaplayer
#Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null){
mediaPlayser.stop();
if (isFinishing()){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
I read docs at Android Developer and still cant pause my service and then start it from the paused state (Playing the song).
With my current code, it always restart from the beginning of the song, but I would like to restart where the song was paused (with the stopClick).
Now I understand problem. In the MainActivity using stopService() method fully destroys it.
So how could i say service to wait when i press stop button to pause?
The Service class:
public class BackgroundSoundService extends Service {
MediaPlayer player;
int currentPos;
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
final int MAX_VOLUME = 100;
player = MediaPlayer.create(this, R.raw.lightmusic);
player.setLooping(true);
float soundVolume = 77;
final float volume = (float) (1 - (Math.log(MAX_VOLUME - soundVolume) / Math
.log(MAX_VOLUME)));
player.setVolume(volume, volume);
player.seekTo(currentPos);
player.start();
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.seekTo(currentPos);
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
player.seekTo(currentPos);
player.start();
}
#Override
public void onRebind(Intent intent) {
player.seekTo(currentPos);
player.start();
super.onRebind(intent);
}
public IBinder onUnBind(Intent arg0) {
// TO DO Auto-generated method
return null;
}
public void onStop() {
currentPos = player.getCurrentPosition();
player.pause();
}
public void onPause() {
currentPos = player.getCurrentPosition();
player.pause();
}
#Override
public void onDestroy() {
currentPos = player.getCurrentPosition();
player.pause();
//player.release();
}
#Override
public void onLowMemory() {
}
}
And my main Activity:
public class MainActivity extends QuizActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
startService(new Intent(this, BackgroundSoundService.class));
}
public void stopClick(View v) {
stopService(new Intent(this, BackgroundSoundService.class));
}
public void playClick(View v) {
startService(new Intent(this, BackgroundSoundService.class));
}
#Override
protected void onResume() {
super.onResume();
}
}