[android] 음악 컨트롤 신호 보내기

헤드셋이 아닌 블루투스 기기로 써드파티 뮤직 앱을 컨트롤 해야하는데, 내가 예전에 이런 걸 해놨었네 ;;;

 

private void putCommand(int event)
{
   try
   {
      IBinder iBinder = (IBinder) Class.forName("android.os.ServiceManager").getDeclaredMethod("checkService", String.class).invoke(null, Context.AUDIO_SERVICE);
      Object audioService = Class.forName("android.media.IAudioService$Stub").getDeclaredMethod("asInterface", IBinder.class).invoke(null, iBinder);

      KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, event);
      Class.forName("android.media.IAudioService").getDeclaredMethod("dispatchMediaKeyEvent", KeyEvent.class).invoke(audioService, keyEvent);

      KeyEvent keyEventUp = new KeyEvent(KeyEvent.ACTION_UP, event);
      Class.forName("android.media.IAudioService").getDeclaredMethod("dispatchMediaKeyEvent", KeyEvent.class).invoke(audioService, keyEventUp);
   }
   catch (Exception e)
   {
      e.printStackTrace();
   }
}

 

 

사용법은

putCommand(KeyEvent.KEYCODE_MEDIA_PLAY); // 재생, 정지
putCommand(KeyEvent.KEYCODE_MEDIA_PREVIOUS); // 이전 곡
putCommand(KeyEvent.KEYCODE_MEDIA_NEXT); // 다음 곡
putCommand(KeyEvent.KEYCODE_MEDIA_FAST_FORWARD); // 빨리 감기
putCommand(KeyEvent.KEYCODE_MEDIA_KEYCODE_MEDIA_REWIND); // 되감기