Android 无法从接收器停止MediaPlayer

Android 无法从接收器停止MediaPlayer,android,broadcastreceiver,android-mediaplayer,Android,Broadcastreceiver,Android Mediaplayer,你好,我想用BroadcastReceiver制作一个当充电状态超过90%时会响的应用程序。我决定首先制作一个应用程序,当Intent.ACTION\u充电器连接且Intent.ACTION\u断开时,它将播放铃声。应用程序将启动铃声,充电器断开连接后将停止铃声 我尝试使用stop()方法停止它 我也试过类似的方法 mp.reset(); mp.prepare(); mp.stop(); mp.release(); mp = null; 当然是在试抓挡 这里我有代码,你可以理解我在做

你好,我想用BroadcastReceiver制作一个当充电状态超过90%时会响的应用程序。我决定首先制作一个应用程序,当Intent.ACTION\u充电器连接且Intent.ACTION\u断开时,它将播放铃声。应用程序将启动铃声,充电器断开连接后将停止铃声

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

 mp.reset();
 mp.prepare();
 mp.stop();
 mp.release();
 mp = null;
当然是在试抓挡

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

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代码中,在第一种情况下创建MediaPlayer对象
案例意图.行动\电源\连接:

 ` @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();

            }

        }`

其他代码与前一个代码相同。完美输出。断开电源后,音乐自动关闭。

谢谢您的回答。两个答案都有效,解决了我的问题。我已经创建了一个函数startMP(),它基本上创建了MediaPlayer对象,我试图删除mp=MediaPlayer.create(context,R.raw.full_charge);第一种情况下的行和代码工作正常。现在它只依赖于startMP()函数。我知道你们两个都看过我的代码,所以我需要编写mp=MediaPlayer.create(context,R.raw.full_charge)行的代码;它是否会对应用程序性能或类似性能产生重大影响。先谢谢你。