J'ai un récepteur qui écoute pour casque MEDIA_PAUSE_PLAY et pour AUDIO_BECOMING_NOISY ils fonctionnent bien si un seul est appelé. Mais certains systèmes Ford Sync enverront une commande de lecture/pause lors de l'extinction de la voiture. Donc, cela a alors 2 récepteurs actifs en même temps et il provoque une force proche parce que j'arrête le lecteur multimédia dans les deux cas. J'ai essayé d'utiliser un booléen, mais d'après ce que j'ai lu, la réception est tuée après chaque événement, donc la valeur booléenne n'est jamais utilisée. Alors, comment puis-je ignorer l'audio devient bruyant si la pause de lecture multimédia est reçue en même temps? Merci d'avance. Voici mon code: package com.joebutt.mouseworldradio;récepteur de diffusion avec plusieurs événements audio
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);
}
}
}
}
}
Voici ma méthode pour arrêter la lecture: public void stopPlaying() { si (mp.isPlaying()) { // lecture d'arrêt et relâchez tout mp .setOnBufferingUpdateListener (null); mp.setOnErrorListener (null); mp.stop(); mp.release(); mp = null; }
Nick, Merci pour la réponse. J'ai déjà essayé ça. Je posterai mon code: – Joeb