android - 无法从接收器停止 MediaPlayer
问题描述
您好,我想使用 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();
}
}
推荐阅读
- c++ - 二维数组的动态分配
- javascript - 如何让游戏继续玩,而不是只玩一次
- node.js - 如何在 json2xls 中添加标题样式?
- rest - 是否可以在 wso2 中翻译协议
- c++ - 从对象 C++ 中的文件读取内容时发生分段错误
- autodesk-forge - NodeJS Forge 查看器错误代码:9。没有可查看的内容
- azure-cosmosdb - 将数组添加到 cosmos graphdb 中的属性
- css - 如何使用 css3 创建进度条
- python - Is there a method to strip specific letter form a string
- c++ - 使用大小写字母的 if else 语句