如何使用语音控制音乐播放器

时间:2013-04-08 06:51:09

标签: android voice audio-player android-music-player

我正在开发一个应用程序,它可以使用Voice控制音乐播放器。例如,当我说“播放”音乐播放器播放歌曲,或说“下一个”音乐播放器将播放下一首歌曲等等。我有谷歌,但没有找到任何想法或源代码。

问题:

  1. 使用语音控制音乐播放器的想法

  2. 源代码示例。

  3. 提前感谢。

    可能相关:Android - Voice Control - Media Intent

1 个答案:

答案 0 :(得分:0)

你需要这个:

    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);
}