Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to volume changes events on Android

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.

like image 299
android developer Avatar asked Jul 03 '12 20:07

android developer


People also ask

How to change volume in Android studio?

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.

What is media volume on Android?

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.


1 Answers

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>
like image 67
Apirak Lunla Avatar answered Sep 21 '22 06:09

Apirak Lunla