2013-04-08 148 views
0

我正在開發一個應用程序,它可以使用語音控制音樂播放器。例如,當我說「播放」音樂播放器播放歌曲,或者說「下一個」音樂播放器將播放下一首歌曲等。我已經Google,但沒有找到任何想法或源代碼。如何使用語音控制音樂播放器

問題:

  1. 理念使用語音

  2. 源代碼示例來控制音樂播放器。

提前致謝。

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

+0

請人幫我嗎? – 2013-04-08 08:29:00

回答

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