【发布时间】:2012-02-18 11:16:17
【问题描述】:
我有一个接收器可以监听耳机 MEDIA_PAUSE_PLAY 和 AUDIO_BECOMING_NOISY,如果只调用一个,它们就可以正常工作。但是一些福特同步系统会在关闭汽车时发送播放/暂停命令。因此,这同时有 2 个接收器处于活动状态,并且它会导致强制关闭,因为我在任何一种情况下都会停止媒体播放器。我曾尝试使用布尔值,但从我读到的内容来看,每次事件后接收都会被杀死,因此布尔值永远不会被使用。那么,如果同时收到媒体播放暂停,我怎么能忽略音频变得嘈杂呢?提前致谢。 这是我的代码: 包 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);
}
}
}
}
}
这是我停止播放的方法: 公共无效停止播放() { 如果(mp.isPlaying()) { //停止播放并释放所有内容 mp.setOnBufferingUpdateListener(null); mp.setOnErrorListener(null); mp.stop(); mp.release(); mp = 空; }
【问题讨论】:
标签: android