0

I'm developing an application, it can control music player using Voice. For example, when i say "play" music player play song, or say "next" music player will play next song, etc..I have Googleed, but didn't find any idea or source code.

Questions:

  1. Idea to control Music Player using Voice

  2. Source code example.

Thank in advance.

Possible related: Android - Voice Control - Media Intent

Community
  • 1
  • 1
Tuan Vu
  • 6,397
  • 2
  • 14
  • 22

1 Answers1

0

You will need this:

    private void playNextMusic() {
    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT));
    sendOrderedBroadcast(i, null);

    i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT));
    sendOrderedBroadcast(i, null);

    playPlayMusic();
}

private void playPreviousMusic() {
    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
    sendOrderedBroadcast(i, null);

    i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS));
    sendOrderedBroadcast(i, null);

    playPlayMusic();
}

private void playPauseMusic() {
    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE));
    sendOrderedBroadcast(i, null);

    i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE));
    sendOrderedBroadcast(i, null);
}

private void playPlayMusic() {
    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
    sendOrderedBroadcast(i, null);

    i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
    sendOrderedBroadcast(i, null);
}

private void playStopMusic() {
    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_STOP));
    sendOrderedBroadcast(i, null);

    i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_STOP));
    sendOrderedBroadcast(i, null);
}