【问题标题】:Android: Handling KEYCODE_MEDIA_NEXT interrupt music playbackAndroid:处理KEYCODE_MEDIA_NEXT中断音乐播放
【发布时间】:2018-01-23 05:41:26
【问题描述】:

题目:处理KEYCODE_MEDIA_NEXT中断音乐播放

在我的 android 应用程序中,我想以自己的方式处理蓝牙扬声器的下一个键。按下键时,我想提高/降低音量。但是每次我按下硬件键时都会中断。中断时间约为半秒。这 应用程序在中断后(而不是之前)接收广播。扬声器是“EasyAcc Mini 2 Portabler 4.1 蓝牙”扬声器。

下面的代码sn-ps展示了实现:

AndroidManifest.xml:

    <receiver android:name=".services.player.BluetoothMediaReceiver">
        <intent-filter android:priority="999999999">
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>

注册蓝牙接收器:

  AudioManager audioManager = (AudioManager)activity.getSystemService(Context.AUDIO_SERVICE);
  Intent i = new Intent(activity, BluetoothMediaReceiver.class);
  PendingIntent pi = PendingIntent.getBroadcast(activity, 1, i, 0);
  audioManager.registerMediaButtonEventReceiver(pi);

BluetoothMediaReceiver 是一个广播接收器

最后是接收方法:

  protected void processReceive(Context context, Intent intent) {
    KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
    if (key.getAction() == KeyEvent.ACTION_DOWN) {
      if (key.getKeyCode() == KeyEvent.KEYCODE_MEDIA_NEXT) {
        volumeChanger.volumeUp();
      }
      else if (key.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
        volumeChanger.volumeDown();
      }
    }
    abortBroadcast();
  }

在 processReceive 方法中创建断点时,音乐中断后命中断点。

我使用 MediaPlayer 播放 mp3 文件中的音乐。当我听不到音乐时,播放器会继续播放(不会暂停)。

谁能给我一个提示以避免被打断。

【问题讨论】:

    标签: android audio bluetooth


    【解决方案1】:

    知道了,问题出在蓝牙扬声器上。

    我用另一个蓝牙耳机测试了它,没有中断。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多