【问题标题】:Android emulate/send ACTION_MEDIA_BUTTON intentAndroid 模拟/发送 ACTION_MEDIA_BUTTON 意图
【发布时间】:2012-10-07 05:07:57
【问题描述】:

我正在尝试通过模拟耳机音乐控件来控制通用音乐播放(播放/暂停、跳过等)。有没有办法广播 ACTION_MEDIA_BUTTON 意图?我尝试了使用 sendBroadcast() 的常规方式,但没有成功。

这可能吗?

【问题讨论】:

    标签: android android-intent media send broadcast


    【解决方案1】:

    试试这个

      long eventtime = SystemClock.uptimeMillis(); 
    
      Intent downIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
      KeyEvent downEvent = new KeyEvent(eventtime, eventtime, 
    KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
      downIntent.putExtra(Intent.EXTRA_KEY_EVENT, downEvent); 
      sendOrderedBroadcast(downIntent, null); 
    
      Intent upIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null); 
      KeyEvent upEvent = new KeyEvent(eventtime, eventtime, 
    KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0); 
      upIntent.putExtra(Intent.EXTRA_KEY_EVENT, upEvent); 
      sendOrderedBroadcast(upIntent, null); 
    

    【讨论】:

    • 感谢您的回复。它似乎不起作用。我尝试使用播放音乐和带和不带耳机的双重扭曲,但它没有做任何事情。是否需要任何特殊权限或任何 Android 版本限制?我正在使用 4.1
    【解决方案2】:

    您可以将不同的 KeyEvent.KEYCODE_MEDIA_* 分配给 AudioManager

    AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, 
    KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    audio.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, 
    KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE));
    

    这仅适用于通过调用监听这些媒体事件的应用

    AudioManager.registerMediaButtonEventReceiver or MediaSession.setMediaButtonReceiver
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2012-07-17
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多