Search code examples
androidbroadcastreceiverandroid-mediaplayer

can not stop MediaPlayer From receiver


hello i want to make an app that will bell when the charging status is above 90 percent , using BroadCastReciever . i decided to first make an app that will play ring when the Intent.ACTION_CHARGER_CONNECTED and Intent.ACTION_DISCONNECTED . app will make start ring and after chargeer disconnected it will stop ring

i have tried to stop it using stop() method i have also tried something similiar like this

 mp.reset();
 mp.prepare();
 mp.stop();
 mp.release();
 mp = null;

of course in try catch block .

here i have code where you can understand what i am doing

MainActivity.java

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    private CustomReciever mReciever = new CustomReciever();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // intent  filetr provide action that a app can get or want to get or listening for broadcast .
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);

        // here we are registering our receiver
        this.registerReceiver(mReciever, filter);
    }

    @Override
    protected void onDestroy() {
         // here we are unregistering our receiver
        unregisterReceiver(mReciever);
        super.onDestroy();
    }

}

CustomReciever

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.widget.Toast;

public class CustomReciever extends BroadcastReceiver {

    MediaPlayer mp;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action != null) {
            String toast = "action is unknown";

            // initializing media player
            mp = MediaPlayer.create(context, R.raw.full_charge);
            switch (action) {
                case Intent.ACTION_POWER_CONNECTED: {
                    toast = "power is connected";
                    startMP(context);
                    }
                    break;
                case Intent.ACTION_POWER_DISCONNECTED: {
                    toast = "power is disconnected";
                    stopMP(context);
                }
                break;
            }
            Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();

        }

    }

    private void startMP(final Context context){
        if(mp == null){
            mp = MediaPlayer.create( context, R.raw.full_charge);
            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    stopMP(context);
                }
            });
        }
        mp.start();
        mp.setLooping(true);
    }

    private void stopMP(Context context) {
        if(mp != null) {
            mp.release();
            mp = null;
            Toast.makeText(context , "song is stopped " , Toast.LENGTH_SHORT).show();
        }
    }
}

Solution

  • In your CustomReceiver Code ,create object ofmediaplayer in first case
    case Intent.ACTION_POWER_CONNECTED:

     ` @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action != null) {
                    String toast = "action is unknown";
    
    
                  switch (action) {
                        case Intent.ACTION_POWER_CONNECTED: {
        // initializing media player
                    mp = MediaPlayer.create(context, R.raw.full_charge);
                            toast = "power is connected";
                            startMP(context);
                            }
                            break;
                        case Intent.ACTION_POWER_DISCONNECTED: {
                            toast = "power is disconnected";
                            stopMP(context);
                        }
                        break;
                    }
                    Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
    
                }
    
            }`
    

    Others code remain same as previous.Perfectly give the output.When disconnected power,music automatically gone off.