Java Code Examples for android.media.AudioManager#dispatchMediaKeyEvent()

The following examples show how to use android.media.AudioManager#dispatchMediaKeyEvent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TabWebContentsDelegateAndroid.java    From delion with Apache License 2.0 5 votes vote down vote up
/**
 * Redispatches unhandled media keys. This allows bluetooth headphones with play/pause or
 * other buttons to function correctly.
 */
@TargetApi(19)
private void handleMediaKey(KeyEvent e) {
    if (Build.VERSION.SDK_INT < 19) return;
    switch (e.getKeyCode()) {
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        case KeyEvent.KEYCODE_MEDIA_CLOSE:
        case KeyEvent.KEYCODE_MEDIA_EJECT:
        case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
            AudioManager am = (AudioManager) mTab.getApplicationContext().getSystemService(
                    Context.AUDIO_SERVICE);
            am.dispatchMediaKeyEvent(e);
            break;
        default:
            break;
    }
}
 
Example 2
Source File: MusicPlayer.java    From always-on-amoled with GNU General Public License v3.0 5 votes vote down vote up
private void sendButton(int keycode) {
    AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keycode);
    am.dispatchMediaKeyEvent(downEvent);
    KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, keycode);
    am.dispatchMediaKeyEvent(upEvent);
}
 
Example 3
Source File: MediaActionService.java    From LibreTasks with Apache License 2.0 5 votes vote down vote up
/**
  * Pause media
  */
 private void pauseMedia() {
   AudioManager audioManager =(AudioManager) getSystemService(Context.AUDIO_SERVICE);
   long eventtime = SystemClock.uptimeMillis() - 1;
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE, 0);
audioManager.dispatchMediaKeyEvent(downEvent);
audioManager.dispatchMediaKeyEvent(upEvent);
  }
 
Example 4
Source File: MediaActionService.java    From LibreTasks with Apache License 2.0 5 votes vote down vote up
/**
  * set the phone to silent
  */
 private void playMedia() {
   AudioManager audioManager =(AudioManager) getSystemService(Context.AUDIO_SERVICE);
   long eventtime = SystemClock.uptimeMillis() - 1;
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
KeyEvent upEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY, 0);
audioManager.dispatchMediaKeyEvent(downEvent);
audioManager.dispatchMediaKeyEvent(upEvent);
  }
 
Example 5
Source File: TabWebContentsDelegateAndroid.java    From AndroidChromium with Apache License 2.0 5 votes vote down vote up
/**
 * Redispatches unhandled media keys. This allows bluetooth headphones with play/pause or
 * other buttons to function correctly.
 */
@TargetApi(19)
private void handleMediaKey(KeyEvent e) {
    if (Build.VERSION.SDK_INT < 19) return;
    switch (e.getKeyCode()) {
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        case KeyEvent.KEYCODE_MEDIA_CLOSE:
        case KeyEvent.KEYCODE_MEDIA_EJECT:
        case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
            AudioManager am = (AudioManager) mTab.getApplicationContext().getSystemService(
                    Context.AUDIO_SERVICE);
            am.dispatchMediaKeyEvent(e);
            break;
        default:
            break;
    }
}
 
Example 6
Source File: TabWebContentsDelegateAndroid.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * Redispatches unhandled media keys. This allows bluetooth headphones with play/pause or
 * other buttons to function correctly.
 */
@TargetApi(19)
private void handleMediaKey(KeyEvent e) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) return;
    switch (e.getKeyCode()) {
        case KeyEvent.KEYCODE_MUTE:
        case KeyEvent.KEYCODE_HEADSETHOOK:
        case KeyEvent.KEYCODE_MEDIA_PLAY:
        case KeyEvent.KEYCODE_MEDIA_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
        case KeyEvent.KEYCODE_MEDIA_STOP:
        case KeyEvent.KEYCODE_MEDIA_NEXT:
        case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
        case KeyEvent.KEYCODE_MEDIA_REWIND:
        case KeyEvent.KEYCODE_MEDIA_RECORD:
        case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
        case KeyEvent.KEYCODE_MEDIA_CLOSE:
        case KeyEvent.KEYCODE_MEDIA_EJECT:
        case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
            AudioManager am = (AudioManager) mTab.getApplicationContext().getSystemService(
                    Context.AUDIO_SERVICE);
            am.dispatchMediaKeyEvent(e);
            break;
        default:
            break;
    }
}
 
Example 7
Source File: ActionReceiver.java    From MediaNotification with Apache License 2.0 4 votes vote down vote up
public void sendKeyPressAudioManager(Context context, int action, int keycode) {
    long uptime = SystemClock.uptimeMillis();

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.dispatchMediaKeyEvent(new KeyEvent(uptime, uptime, action, keycode, 0));
}
 
Example 8
Source File: AudioHelper.java    From Noyze with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void dispatchMediaKeyEvent(AudioManager manager, KeyEvent event) {
    manager.dispatchMediaKeyEvent(event);
}
 
Example 9
Source File: AudioHelper.java    From Noyze with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.KITKAT)
public static void dispatchMediaKeyEvent(AudioManager manager, KeyEvent event) {
    manager.dispatchMediaKeyEvent(event);
}