Faisal Naseer
Faisal Naseer

Reputation: 4248

Audio is playing twice in Android Media Player

In my application I'm having a method responsible for playing file placed under raw directory. But when Ever I call that function in my onResume() method, sound is played twice. Even I have googled and tried different solutions. even by checking mediaPlayer.isPlaying() and then stopping the MediaPlayer instance but still didn't get any help.

private void EnglishSound(){
        if(mediaPlayer1!=null){
            if(mediaPlayer1.isPlaying()){
                mediaPlayer1.stop();

            }
            mediaPlayer1.reset();
             mediaPlayer1.release();
        }

        mediaPlayer1 = MediaPlayer.create(this, R.raw.p012);

            mediaPlayer1.start();

    }

 public void onResume() {
        super.onResume();
       EnglishSound();

    }

and EnglishSound() is called no where else in the whole activity. Even have tried debugging but it never enters the if block containing isPlaying().

Upvotes: 0

Views: 1090

Answers (1)

T D Nguyen
T D Nguyen

Reputation: 7603

Try to release onPause()

 public void onPause() {
    super.onPause();
    if(mediaPlayer1 != null)
      mediaPlayer1.release();
}

Upvotes: 3

Related Questions