1

I want to play sound in my android app. And maybe I have to use service for this. But from this question I see on home button pressed and on screen lock onPause method called. But in case of home button pressed I want to stop sound in case of screen lock i dont want to stop sound. So how to do this and how to use onPause method in this case?

Please someone help me. Thanks...

John R
  • 2,078
  • 8
  • 35
  • 58
  • It's hard to imagine your use case. Will the user expect such a behaviour? The fact that you handle these events in callbacks like `onPause` and `onStop` is here to abstract the reason why your app is not in foreground anymore, because it's not really supposed to behave differently – Joffrey Apr 10 '14 at 12:09
  • Anyway, if you really want to distinguish, maybe you could look at this: http://stackoverflow.com/a/7331905/1540818 – Joffrey Apr 10 '14 at 12:14
  • You will need to be clearer about your stop / don't stop cases. For example, what should happen if the user navigates away to another Activity, potentially from another app? – Chris Stratton Apr 10 '14 at 13:46

1 Answers1

1

Use MediaPlayer from Android. See: Android Example

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Ferzinha Fil
  • 142
  • 6