Is there any way to listen to the event of volume change on Android, without just taking over the volume buttons?
The only thing I've found that works is here, but it works only after the volume control has disappeared.
Not all devices have volume buttons, and I need to capture the volume changes as soon as they occur, and not after the volume dialog is gone.
By default, pressing the volume control modifies the volume of the active audio stream. If your app isn't currently playing anything, hitting the volume keys adjusts the music volume (or the ringer volume before Android 9). Unless your app is an alarm clock, you should play audio with usage AudioAttributes.
Under "Media volume," tap Play media to. When you press a volume button, the volume that changes depends on what you're doing. For example, if you're watching a movie, the movie volume changes. If you press your volume buttons when you're not listening to anything, your media volume changes.
Use broadcast receiver VOLUME_CHANGED_ACTION then use AudioManager to obtain current volume.
<receiver android:name="VolumeChangeReceiver" >
<intent-filter>
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With