Hi, I want my app to run a service in the background playing a sound.
I created this service and when it create’s it is set to start the media player.
I want the media player to stop simply by stopping the service.
In my activity i have startService called and that starts the music but when I call stopService I have to force close.
If I put a simple Toast in the onDestroy than the whole thing works like a charm but I still have the problem of the sound hasn’t stopped.
Here is the code from my service..
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MyService extends Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
MediaPlayer playerlayer = MediaPlayer.create(MyService.this, R.raw.my_sound);
player.start();
player.setLooping(true);
}
@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}
}