title: Media Player not playing sound properly in Android on charger connection
body: <p>I am new to software developing and i am trying to make an application that will play a sound when it charges. The first time i connect it to the charging cable, it plays the sound two times, which is not expected. Also, when i connect and disconnect the cable too quick, it also plays it twice.
  Does anyone know the solution to this problem?</p>
  
  <pre><code>package com.mccharginggimmick;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.IntentFilter;
  import android.media.MediaPlayer;
  import android.os.BatteryManager;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.ImageView;
  
  public class MainActivity extends AppCompatActivity {
  
  IntentFilter intentFilter;
  BroadcastReceiver broadcastReceiver;
  MediaPlayer mediaPlayerOff;
  MediaPlayer mediaPlayerOn;
  boolean playedOnceOn;
  boolean playedOnceOff;
  ImageView imageView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  
      View decorView = getWindow().getDecorView();
      int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
      decorView.setSystemUiVisibility(uiOptions);
  
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
  
      imageView = (ImageView) findViewById(R.id.imageView);
      intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      broadcastReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
              boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
  
              if(isCharging){
              onCharge();
              }
              else{
              onDisconnect();
              }
          }
      };
      MainActivity.this.registerReceiver(broadcastReceiver, intentFilter);
  }
  public void onCharge(){
  
      if(!playedOnceOn){
          imageView.setVisibility(View.INVISIBLE);
      mediaPlayerOn = MediaPlayer.create(this, R.raw.btnon);
      mediaPlayerOn.start();
      mediaPlayerOn.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mediaPlayer) {
              mediaPlayerOn.stop();
              playedOnceOn = true;
              playedOnceOff = false;
          }
      });
      }
  }
  public void onDisconnect(){
  
      if(!playedOnceOff){
          imageView.setVisibility(View.VISIBLE);
      mediaPlayerOff = MediaPlayer.create(this, R.raw.btnoff);
      mediaPlayerOff.start();
      mediaPlayerOff.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
          @Override
          public void onCompletion(MediaPlayer mediaPlayer) {
              mediaPlayerOff.stop();
              playedOnceOff = true;
              playedOnceOn = false;
          }
      });
      }
  }
  }
  </code></pre>