0

Can anyone help me with my problem, i am trying to play a background music automatically without a button when activity started.

I dont have any idea how.

Karthik Kumar
  • 1,375
  • 1
  • 12
  • 29
  • 1
    duplicate of https://stackoverflow.com/questions/27579765/play-background-music-in-all-activities-of-android-app – Karthik Kumar Dec 08 '17 at 14:23
  • 3
    Possible duplicate of [Play background music in all activities of Android app](https://stackoverflow.com/questions/27579765/play-background-music-in-all-activities-of-android-app) – Sam Twidale Dec 08 '17 at 14:36

2 Answers2

0

Add music file in the raw folder then call R.raw.[musincname] then call function in your onCreate or onStart,....

public static void PlayVoice(final Context context, int rawVoice) {
    voice = MediaPlayer.create(context, rawVoice);
    voice.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            if (voice != null) {
                voice.release();
            }
        }
    });
    voice.start();
}
Hamza rasaee
  • 362
  • 4
  • 12
0
 public void audioPlayer(String path, String fileName){
//set up MediaPlayer    
MediaPlayer mp = new MediaPlayer();

try {
    mp.setDataSource(path + File.separator + fileName);
    mp.prepare();
    mp.start();
} catch (Exception e) {
    e.printStackTrace();
}

}