1

I'm making a small android app like image below: enter image description here

I have a sound button. All I want is background music will play across all activity and when I click on sound button, music will stop. Or click to play music again.

I dont know how to make it. PLease help me! or give me link to refer.

I'd gladly appreciate your help it Thanks!

Jason C
  • 38,729
  • 14
  • 126
  • 182
PLorida
  • 103
  • 1
  • 2
  • 11
  • Have you tried overriding the Application and using the MediaPlayer in there? Or using a service? – Cruceo Apr 21 '14 at 17:49
  • possible duplicate of [Playing BG Music Across Activities in Android](http://stackoverflow.com/questions/2097909/playing-bg-music-across-activities-in-android) – Jason C Apr 21 '14 at 17:49
  • ^ Please read through *all* the answers there and take the accepted answer with a grain of salt (based on the comments). See also the example at http://developer.android.com/guide/components/fundamentals.html#appcomp. – Jason C Apr 21 '14 at 17:51
  • Possibly a better duplicate (with a better answer): https://stackoverflow.com/questions/8209858/android-background-music-service?rq=1 – Jason C Apr 21 '14 at 17:52
  • I tried service. But it set what activity play music and what activity stop music..All i want is click the button and music background will be played or stopped – PLorida Apr 21 '14 at 17:55

2 Answers2

3

Hi please Follow below steps

  1. initialized
    MusicPlayer object when your app starts MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);

set this in your button's click event.

  1. Start it by mediaPlayer.start();

3.Stop it by mediaPlayer.stop();

but when you stopping mediaPlayer before that just check mediaPlayer.isPlaying() with if Condition. then Stop. else error thrown.

or follow this Example Turorial Link

Ando Masahashi
  • 3,112
  • 2
  • 24
  • 41
2

In order for media to be played in the background of your app location when the user is interacting with it you must start a Service from your application's main activity and the service shall contain all the methods related to playback. To allow activity to interact with the service, a service connection is also required. In short, we need to implement a bounded service.

Refer http://www.codeproject.com/Articles/258176/Adding-Background-Music-to-Android-App

Shreyos Adikari
  • 12,348
  • 19
  • 73
  • 82