2012-02-01 4 views
0

헤드셋 MEDIA_PAUSE_PLAY를 청취하는 리시버가 있고 AUDIO_BECOMING_NOISY의 경우 리시버가 하나만 호출되면 제대로 작동합니다. 그러나 일부 포드 동기화 시스템은 자동차를 끌 때 재생/일시 중지 명령을 보냅니다. 그래서 이것은 동시에 2 개의 리시버가 동시에 활성화되어 있으며 두 상황 모두에서 미디어 플레이어를 멈추기 때문에 강제 종료됩니다. 내가 부울 값을 사용하지 않으므로 부울을 사용하여 노력했지만 각 수신 이벤트가 종료 된 후 수신 결과를 읽지 못했습니다. 그렇다면 미디어 재생 일시 정지가 동시에 수신되면 잡음이 들리는 오디오를 어떻게 무시할 수 있습니까? 미리 감사드립니다. 내 코드는 다음과 같습니다. 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

문제를 해결하기 위해 미디어 플레이어가 시끄러운 청취자가되는 오디오에 대해 null인지 확인했습니다. 이로써 더 이상 존재하지 않는 미디어 플레이어를 막을 수 없었습니다. 이제 내 동기화 시스템에서 잘 작동합니다.

1

두 개의 수신기를 동시에 활성화하는 것이 좋습니다.

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

가 재생되는 경우에만 미디어 플레이어를 중지 그 방법 :이 문제는 이미 수신기에서 이것을 시도 중지 할 때 미디어 플레이어를 중지하려고하는 것입니다 경우. 그렇지 않은 경우 코드를 게시하여 정확하게 무엇을 시도하는지 확인할 수 있습니까?

+0

Nick, 응답 해 주셔서 감사합니다. 나는 이미 그것을 시도했다. 내 코드를 게시 할 예정입니다. – Joeb

관련 문제