【问题标题】:broadcast receiver with multiple audio events具有多个音频事件的广播接收器
【发布时间】: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


    【解决方案1】:

    同时激活两个接收器应该没问题。如果问题是您在媒体播放器已经停止时尝试停止它,请在您的接收器中尝试:

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

    这样您只有在播放时才停止媒体播放器。如果不是这种情况,您能否发布代码,以便我们准确了解您正在尝试的内容。

    【讨论】:

    • 尼克,感谢您的回复。我已经试过了。我将发布我的代码:
    【解决方案2】:

    为了解决这个问题,我检查了媒体播放器是否因音频变得嘈杂的听众而为空。这阻止了它试图停止不再存在的媒体播放器。现在它与我的同步系统配合得很好。

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-14
      • 2023-03-04
      相关资源
      最近更新 更多