无法停止 MediaPlayer From receiver

can not stop MediaPlayer From receiver

你好,我想使用 BroadCastReciever 制作一个应用程序,当充电状态高于 90% 时会响铃。我决定首先制作一个在 Intent.ACTION_CHARGER_CONNECTED 和 Intent.ACTION_DISCONNECTED 时播放铃声的应用程序。应用程序将开始响铃,充电器断开后它将停止响铃

我已经尝试使用 stop() 方法停止它 我也试过类似的东西

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

当然在 try catch 块中。

这里有代码,你可以理解我在做什么

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();
    }

}

自定义接收器

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();
        }
    }
}

您每次接收都在创建 Mediaplayer 对象,请尝试更新代码

 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: {
                    toast = "power is connected";
                     // initializing media player
                    mp = MediaPlayer.create(context, R.raw.full_charge);
                    startMP(context);
                    }
                    break;
                case Intent.ACTION_POWER_DISCONNECTED: {
                    toast = "power is disconnected";
                    if(mp!=null)
                    stopMP(context);
                }
                break;
            }
            Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();

        }

    }

在您的 CustomReceiver 代码中,在第一种情况下创建媒体播放器对象
案例 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();

            }

        }`

其他代码与previous.Perfectly相同,给output.When断开电源,音乐自动关闭。