无法停止来自接收器的MediaPlayer

问题描述 投票:0回答:2

您好,我想使用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();
    }

}

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();
        }
    }
}
android broadcastreceiver android-mediaplayer
2个回答
0
投票

您每次接收都会创建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();

        }

    }

0
投票

在您的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();

            }

        }`

其他代码与以前相同。完全输出。断开电源后,音乐自动熄灭。

© www.soinside.com 2019 - 2024. All rights reserved.