I am using following service
to play sound in my app when app start sound start and when user click on any button
in menu its stop. But I am facing some problems. Like if user open app and without pressing any button
in app menu he press menu button
on mobile sound not stop. And if app start and call or message is coming on mobile still sound play in background. How to stop service
on these 2 events?
Service code-
public class PlayAudio extends Service{
private static final String LOGCAT = null;
MediaPlayer objPlayer;
public void onCreate(){
super.onCreate();
Log.d(LOGCAT, "Service Started!");
objPlayer = MediaPlayer.create(this, R.raw.test);
}
public int onStartCommand(Intent intent, int flags, int startId){
objPlayer.start();
Log.d(LOGCAT, "Media Player started!");
if(objPlayer.isLooping() != true){
Log.d(LOGCAT, "Problem in Playing Audio");
}
return 1;
}
public void onStop(){
objPlayer.stop();
objPlayer.release();
}
public void onPause(){
objPlayer.stop();
objPlayer.release();
}
public void onDestroy(){
objPlayer.stop();
objPlayer.release();
}
@Override
public IBinder onBind(Intent objIndent) {
return null;
}
}
When my app start from splash activity I starting audio like-
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
objIntent = new Intent(this, PlayAudio.class);
startService(objIntent);
new Handler().postDelayed(csRunnable2, 5000);
}
Then in main menu activity stopping it when user press any button-
hist = (Button) findViewById(R.id.hist);
hist.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
objIntent = new Intent(MainActivity.this, PlayAudio.class);
stopService(objIntent);
startActivity(new Intent(MainActivity.this, History.class));
finish();
And please tell me where I have to do changes?