mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-03-18 04:01:29 +00:00
make conversational awareness volume change smoother and improve media control by ear detection
This commit is contained in:
@@ -534,9 +534,15 @@ class AirPodsService: Service() {
|
|||||||
MediaController.userPlayedTheMedia = false
|
MediaController.userPlayedTheMedia = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.d(
|
||||||
|
"AirPods Parser",
|
||||||
|
"inEarData: ${inEarData.sorted()}, newInEarData: ${newInEarData.sorted()}"
|
||||||
|
)
|
||||||
if (newInEarData.sorted() == inEarData.sorted()) {
|
if (newInEarData.sorted() == inEarData.sorted()) {
|
||||||
|
Log.d("AirPods Parser", "hi")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Log.d("AirPods Parser", "this shouldn't be run if the last log was 'hi'.")
|
||||||
|
|
||||||
inEarData = newInEarData
|
inEarData = newInEarData
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
package me.kavishdevar.aln.utils
|
package me.kavishdevar.aln.utils
|
||||||
|
|
||||||
import android.media.AudioManager
|
import android.media.AudioManager
|
||||||
@@ -31,6 +30,7 @@ object MediaController {
|
|||||||
private lateinit var audioManager: AudioManager
|
private lateinit var audioManager: AudioManager
|
||||||
var iPausedTheMedia = false
|
var iPausedTheMedia = false
|
||||||
var userPlayedTheMedia = false
|
var userPlayedTheMedia = false
|
||||||
|
private val handler = Handler(Looper.getMainLooper())
|
||||||
|
|
||||||
fun initialize(audioManager: AudioManager) {
|
fun initialize(audioManager: AudioManager) {
|
||||||
this.audioManager = audioManager
|
this.audioManager = audioManager
|
||||||
@@ -43,7 +43,7 @@ object MediaController {
|
|||||||
Log.d("MediaController", "Playback config changed, iPausedTheMedia: $iPausedTheMedia")
|
Log.d("MediaController", "Playback config changed, iPausedTheMedia: $iPausedTheMedia")
|
||||||
if (configs != null && !iPausedTheMedia) {
|
if (configs != null && !iPausedTheMedia) {
|
||||||
Log.d("MediaController", "Seems like the user changed the state of media themselves, now I won't `play` until the ear detection pauses it.")
|
Log.d("MediaController", "Seems like the user changed the state of media themselves, now I won't `play` until the ear detection pauses it.")
|
||||||
Handler(Looper.getMainLooper()).postDelayed({
|
handler.postDelayed({
|
||||||
iPausedTheMedia = !audioManager.isMusicActive
|
iPausedTheMedia = !audioManager.isMusicActive
|
||||||
userPlayedTheMedia = audioManager.isMusicActive
|
userPlayedTheMedia = audioManager.isMusicActive
|
||||||
}, 7) // i have no idea why, but android sends a pause event a hundred times after the user does something.
|
}, 7) // i have no idea why, but android sends a pause event a hundred times after the user does something.
|
||||||
@@ -99,10 +99,8 @@ object MediaController {
|
|||||||
if (initialVolume == null) {
|
if (initialVolume == null) {
|
||||||
initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
initialVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
|
||||||
Log.d("MediaController", "Initial Volume Set: $initialVolume")
|
Log.d("MediaController", "Initial Volume Set: $initialVolume")
|
||||||
audioManager.setStreamVolume(
|
val targetVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 1 / 12
|
||||||
AudioManager.STREAM_MUSIC,
|
smoothVolumeTransition(initialVolume!!, targetVolume)
|
||||||
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 1 / 12, 0
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
Log.d("MediaController", "Initial Volume: $initialVolume")
|
Log.d("MediaController", "Initial Volume: $initialVolume")
|
||||||
}
|
}
|
||||||
@@ -110,10 +108,25 @@ object MediaController {
|
|||||||
@Synchronized
|
@Synchronized
|
||||||
fun stopSpeaking() {
|
fun stopSpeaking() {
|
||||||
Log.d("MediaController", "Stopping speaking, initialVolume: $initialVolume")
|
Log.d("MediaController", "Stopping speaking, initialVolume: $initialVolume")
|
||||||
initialVolume?.let { volume ->
|
if (initialVolume != null) {
|
||||||
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0)
|
smoothVolumeTransition(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC), initialVolume!!)
|
||||||
initialVolume = null // Reset to null after restoring the volume
|
initialVolume = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun smoothVolumeTransition(fromVolume: Int, toVolume: Int) {
|
||||||
|
val step = if (fromVolume < toVolume) 1 else -1
|
||||||
|
val delay = 50L // 50 milliseconds delay between each step
|
||||||
|
var currentVolume = fromVolume
|
||||||
|
|
||||||
|
handler.post(object : Runnable {
|
||||||
|
override fun run() {
|
||||||
|
if (currentVolume != toVolume) {
|
||||||
|
currentVolume += step
|
||||||
|
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0)
|
||||||
|
handler.postDelayed(this, delay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user