2012-02-01 45 views
0

我有一個接收器,用於監聽頭戴式耳機MEDIA_PAUSE_PLAY和AUDIO_BECOMING_NOISY,如果只有一個被調用,它們就可以正常工作。但是一些福特Sync系統會在關閉汽車時發送播放/暫停命令。因此,此時有兩個接收器同時處於活動狀態,並且會導致力量接近,因爲我正在停止媒體播放器。我嘗試過使用布爾值,但是從接收到的內容開始,每個事件都會被殺死,所以布爾值永遠不會被使用。那麼,如果同時接收到媒體播放暫停,我怎麼能忽略音頻變得嘈雜?提前致謝。這裏是我的代碼: package 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); 
      } 

     } 
    } 
} 

}

這裏是我的方法,停止播放: 公共無效stopPlaying(){ 如果 (mp.isPlaying()){ // 停止播放並釋放一切 MP .setOnBufferingUpdateListener(NULL); mp.setOnErrorListener(null); mp.stop(); mp.release(); mp = null; }

回答

0

爲了解決這個問題,我檢查了媒體播放器是否爲空音頻變成嘈雜的監聽器。這阻止了它試圖阻止不再存在的媒體播放器。現在,它對我的​​同步系統非常有用。

1

應該可以讓兩個接收器同時激活。如果問題是,你正在試圖阻止媒體播放器時,它已經停止試試這個在您的接收器:

if (mp.isPlaying()) { 
     mp.stop(); 
    } 

這樣,你只有在停止播放的媒體播放器。如果情況並非如此,您是否可以發佈代碼,以便我們可以看到您正在嘗試的內容。

+0

尼克,謝謝你的迴應。我已經嘗試過了。我會發布我的代碼: – Joeb 2012-02-01 19:14:35