i have a problem .. i used a MediaPlayer class to start a song and the problem is that when the app is closed the song playback also stops.
I search the net and found i can override on back pressed method to move task to background, it worked fine for me but , when i remove the app from recent apps (by swiping the recent apps or clearing the recent apps) the song playback stops.
So is there any way to prevent this.
My code is like this :
Main Activity :
oncreate()
{
//
public static MediaPlayer mp;
startsong();
}
void startsong()
{
mp = MediaPlayer.create(....);
mp.start();
Intent intent = new Intent(this,Service.class);
startService(inent);
}
onBackPressed()
{
moveTaskToBack(true);
}
Service Class :
onStartCommand()
{
Main.mp.setOnCompletitionListener( ... new oncomp ...)
{
Main.mp.stop();
Main.mp.release();
}
}
Edit : I found the answer , the answer is to stick the service with a notification in foreground