Method for Android to modify system volume using AudioManager

This article gives an example of how Android uses AudioManager to modify the system volume. Share with you for your reference, as follows:

The following describes several methods of volume adjustment of AudioManager

First, get the AudioManager instance:

There are two methods to adjust the volume, one is progressive, that is, increase or decrease step by step like pressing the volume key manually, and the other is to set the volume value directly

1. Progressive

Explain the three parameters

The first streamtype is the type that needs to adjust the volume. Here is the media volume, which can be:

STREAM_ Alarm stream_ Music music playback is media volume stream_ Notification window top status bar notification, stream_ Ring stream_ System system stream_ VOICE_ Call stream_ DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF DTMF

The second direction is the direction of adjustment, increase or decrease, which can be:

ADJUST_ Lower volume adjust_ Raise volume adjust_ Same remains unchanged, which is mainly used to show the user the current volume

The third flag is some additional parameters. Only two commonly used flags are introduced

FLAG_ PLAY_ Sound plays the sound flag when the volume is adjusted_ SHOW_ The volume bar is displayed during UI adjustment, that is, the one that appears when you press the volume key

2. How to set the volume value directly:

The first and third parameters are the same as above

The second parameter is an int value of volume. Getstreammaxvolume (int streamtype) gets the maximum volume of this type. You can calculate the volume you need according to this value. I'll directly adjust it to the maximum here

Finally, my code:

Of course, set permissions

PS: for details on Android manifest function and permission settings, please refer to our online tools:

Description of Android manifest functions and permissions:

http://tools.jb51.net/table/AndroidManifest

For more Android related content, readers who are interested can view the topics on this site: summary of Android view skills, summary of activity operation skills of Android programming, summary of SQLite database skills for Android, summary of JSON format data skills for Android, summary of Android database operation skills, summary of Android file operation skills @ l)u 419)u 7@, introduction and advanced tutorial of Android development, summary of Android resource operation skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>