0
我正在實現媒體播放器,並嘗試暫停/啓動播放器。但是當我開始耳機按鈕按下時,廣播接收器會重複調用。我正在使用下面的代碼。有誰能夠幫助我。如何按耳機按鈕時暫停MediaPlayer?
編輯:
我已經初始化接收器像this.any事錯在我的代碼
MediaButtonIntentReceiver mMediaButtonReceiver = new MediaButtonIntentReceiver();
IntentFilter mediaFilter = new IntentFilter(
Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(100000);
registerReceiver(mMediaButtonReceiver, mediaFilter);
我的廣播接收器:
public class MediaButtonIntentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
int action = event.getAction();
if (action == KeyEvent.ACTION_DOWN) {
if(AudioPlayer.player!=null)
{
if(AudioPlayer.player.isPlaying())
AudioPlayer.player.pause();
else
AudioPlayer.player.start();
}
}
abortBroadcast();
}
}
請說明爲什麼你放棄投票this.I必須改善我的問題,以獲得更好的答案。 – 2012-01-05 11:19:48
你是什麼意思,它被反覆調用?您的代碼是否與您鏈接的示例中顯示的代碼完全相同?你記得要過濾意圖嗎? – Cheesebaron 2012-01-05 11:25:25