mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-02-19 07:37:00 +00:00
implement music play/pause from ear detection
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package me.kavishdevar.aln
|
||||
|
||||
import android.media.AudioManager
|
||||
import android.view.KeyEvent
|
||||
|
||||
class MediaController (private val audioManager: AudioManager){
|
||||
fun sendPause() {
|
||||
if (audioManager.isMusicActive) {
|
||||
audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PAUSE))
|
||||
audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PAUSE))
|
||||
}
|
||||
}
|
||||
|
||||
fun sendPlay() {
|
||||
if (!audioManager.isMusicActive) {
|
||||
audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY))
|
||||
audioManager.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY))
|
||||
}
|
||||
}
|
||||
var initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
||||
fun startSpeaking() {
|
||||
if (!audioManager.isMusicActive) {
|
||||
// reduce volume to 10% of initial volume
|
||||
initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
||||
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (initialVolume * 0.1).toInt(), 0)
|
||||
}
|
||||
}
|
||||
|
||||
fun stopSpeaking() {
|
||||
if (!audioManager.isMusicActive) {
|
||||
// restore initial volume
|
||||
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, initialVolume, 0)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user