-
-
Notifications
You must be signed in to change notification settings - Fork 136
Add volume controls to media skill #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,18 +17,79 @@ class MediaSkill(correspondingSkillInfo: SkillInfo, data: StandardRecognizerData | |||||||||||||||||||||||||||||||||
| val audioManager = getSystemService(ctx.android, AudioManager::class.java) | ||||||||||||||||||||||||||||||||||
| ?: return MediaOutput(performedAction = null) // no media session found | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| val key = when (inputData) { | ||||||||||||||||||||||||||||||||||
| is Media.Play -> KeyEvent.KEYCODE_MEDIA_PLAY | ||||||||||||||||||||||||||||||||||
| is Media.Pause -> KeyEvent.KEYCODE_MEDIA_PAUSE | ||||||||||||||||||||||||||||||||||
| is Media.Previous -> KeyEvent.KEYCODE_MEDIA_PREVIOUS | ||||||||||||||||||||||||||||||||||
| is Media.Next -> KeyEvent.KEYCODE_MEDIA_NEXT | ||||||||||||||||||||||||||||||||||
| when (inputData) { | ||||||||||||||||||||||||||||||||||
| is Media.Play -> { | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY)) | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.Pause -> { | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE)) | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.Previous -> { | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PREVIOUS)) | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PREVIOUS)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.Next -> { | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_NEXT)) | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_NEXT)) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.VolumeUp -> { | ||||||||||||||||||||||||||||||||||
| audioManager.adjustStreamVolume( | ||||||||||||||||||||||||||||||||||
| AudioManager.STREAM_MUSIC, | ||||||||||||||||||||||||||||||||||
| AudioManager.ADJUST_RAISE, | ||||||||||||||||||||||||||||||||||
| AudioManager.FLAG_SHOW_UI | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.VolumeDown -> { | ||||||||||||||||||||||||||||||||||
| audioManager.adjustStreamVolume( | ||||||||||||||||||||||||||||||||||
| AudioManager.STREAM_MUSIC, | ||||||||||||||||||||||||||||||||||
| AudioManager.ADJUST_LOWER, | ||||||||||||||||||||||||||||||||||
| AudioManager.FLAG_SHOW_UI | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.VolumeUpTimes -> { | ||||||||||||||||||||||||||||||||||
| val times = extractNumberFromString(ctx, inputData.times) | ||||||||||||||||||||||||||||||||||
| repeat(times) { | ||||||||||||||||||||||||||||||||||
| audioManager.adjustStreamVolume( | ||||||||||||||||||||||||||||||||||
| AudioManager.STREAM_MUSIC, | ||||||||||||||||||||||||||||||||||
| AudioManager.ADJUST_RAISE, | ||||||||||||||||||||||||||||||||||
| if (it == 0) AudioManager.FLAG_SHOW_UI else 0 | ||||||||||||||||||||||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| is Media.VolumeDownTimes -> { | ||||||||||||||||||||||||||||||||||
| val times = extractNumberFromString(ctx, inputData.times) | ||||||||||||||||||||||||||||||||||
| repeat(times) { | ||||||||||||||||||||||||||||||||||
| audioManager.adjustStreamVolume( | ||||||||||||||||||||||||||||||||||
| AudioManager.STREAM_MUSIC, | ||||||||||||||||||||||||||||||||||
| AudioManager.ADJUST_LOWER, | ||||||||||||||||||||||||||||||||||
| if (it == 0) AudioManager.FLAG_SHOW_UI else 0 | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, key)) | ||||||||||||||||||||||||||||||||||
| audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, key)) | ||||||||||||||||||||||||||||||||||
| return MediaOutput(performedAction = inputData) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private fun extractNumberFromString(ctx: SkillContext, input: String?): Int { | ||||||||||||||||||||||||||||||||||
| if (input.isNullOrBlank()) { | ||||||||||||||||||||||||||||||||||
| return 1 | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return ctx.parserFormatter!! | ||||||||||||||||||||||||||||||||||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will crash if there is no number parser. You should disable the volume up/down sentences for this skill if the number parser for the current language is null. I would suggest adding a |
||||||||||||||||||||||||||||||||||
| .extractNumber(input) | ||||||||||||||||||||||||||||||||||
| .mixedWithText | ||||||||||||||||||||||||||||||||||
| .asSequence() | ||||||||||||||||||||||||||||||||||
| .filterIsInstance<org.dicio.numbers.unit.Number>() | ||||||||||||||||||||||||||||||||||
| .filter { it.isInteger } | ||||||||||||||||||||||||||||||||||
| .map { it.integerValue().toInt() } | ||||||||||||||||||||||||||||||||||
| .firstOrNull() | ||||||||||||||||||||||||||||||||||
| ?.coerceIn(1, 10) // Limit to 1-10 steps for safety | ||||||||||||||||||||||||||||||||||
| ?: 1 | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+82
to
+90
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| companion object { | ||||||||||||||||||||||||||||||||||
| val TAG: String = MediaSkill::class.simpleName!! | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe deduplicate this (and the below) with some member function