我有一个侦听耳机MEDIA_PAUSE_PLAY的接收器,对于AUDIO_BECOMING_NOISY,如果只调用一个,它们可以正常工作。但是一些福特Sync系统会在关闭汽车时发送播放/暂停命令。因此,这有两个接收器同时处于活动状态并且它会导致力关闭,因为我在任何一种情况下都停止了媒体播放器。我已经尝试使用布尔值,但是从我读过的内容中,每个事件后都会杀死接收器,因此布尔值永远不会被使用。那么如果同时收到媒体播放暂停,如何忽略音频变得嘈杂?提前致谢。 这是我的代码: 包com.joebutt.mouseworldradio;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.view.KeyEvent;
public class RemoteControlReceiver extends BroadcastReceiver
{
//I created stopCounter to try and keep this from running more than 1 time
int stopCounter = 0;
//I created mediaAction to try and keep both receivers from activating
boolean mediaAction = false;
@Override
public void onReceive(Context context, Intent intent)
{
//boolean mediaAction = false;
//int stopCounter = 0;
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction()))
{
mediaAction = true;
//stopCounter = 1;
if (stopCounter < 1)
{
//mediaAction = true; force closes here to
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE == event.getKeyCode())
{
stopCounter = 1;
//mediaAction only works here if you hit the stop button 1 time, then it will work the next time you shut the car off
mediaAction = true;
//stop and release the media player
if (Play.mp.isPlaying())
{
Play playService = new Play();
playService.stopPlaying();
//stop the play service
Intent stopPlayingService = new Intent(context, Play.class);
context.stopService(stopPlayingService);
//switch back to the main screen
Intent showMain = new Intent(context, MouseWorldRadioActivity.class);
showMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(showMain);
}
}
}
}
else if (!mediaAction)
{
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction()))
{
if (Play.mp.isPlaying())
{
//stop and release the mediaplayer
Play playService = new Play();
playService.stopPlaying();
//}
//stop the play service
Intent stopPlayingService = new Intent(context, Play.class);
context.stopService(stopPlayingService);
//switch back to the main screen
Intent showMain = new Intent(context, MouseWorldRadioActivity.class);
showMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(showMain);
}
}
}
}
}
这是我停止播放的方法: public void stopPlaying() { if(mp.isPlaying()) { //停止播放并释放所有内容 mp.setOnBufferingUpdateListener(NULL); mp.setOnErrorListener(NULL); mp.stop(); mp.release(); mp = null; }
答案 0 :(得分:1)
两个接收器同时处于活动状态应该没问题。如果问题是您试图在媒体播放器已经停止时停止播放,请在接收器中尝试此操作:
if (mp.isPlaying()) {
mp.stop();
}
这样你只会在媒体播放器正在播放时停止播放。如果不是这样,你可以发布代码,这样我们就可以确切地看到你正在尝试的东西。
答案 1 :(得分:0)
为了解决这个问题,我检查了媒体播放器是否为空,因为音频变成了嘈杂的听众。这阻止了它试图阻止不再存在的媒体播放器。现在它适用于我的同步系统。