How can I make background music for android project

PLorida picture PLorida · Apr 21, 2014 · Viewed 9.9k times · Source

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!

Answer

Ando Masahashi picture Ando Masahashi · Apr 21, 2014

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