I'm really new on android and java. I'm making an app and it has a media service and I want the media to be stopped or paused on incoming calls. This is my media service code
public class ServiceMusic extends Service {
MediaPlayer music;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
music.stop();
music.release();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
if (music !=null)
music.stop();
music = MediaPlayer.create(ServiceMusic.this, R.raw.music);
music.start();
music.setLooping(true);
}
}
I will really appreciate if you can help me with this , and if you can give me a complete instruction that will be great. Thanks
you have to create receiver for incoming call like below:
in manifest put this:
permission :
make your media player variable static & globale, in reciver call another service to check your media player is running or not. if it is running then stop it. you can use also use same service.
you have to start services from receiver:
in service use this:
check edited ans:
In below example I have done all things. there is one button it ll play song in service. at that time you receive in coming call then it will stop your service(stop playing song) and when you reject or drop call then it again start playing song
main Activity class :
BackgroundSoundService class:
call receiver class:
extra class for static member:
in last manifest file:
All code is working at my end. please check it and tell me.