Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use AudioManager

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use AudioManager". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use AudioManager.

When we are listening to music, when we are browsing small videos, when we are watching anime, watching movies, or chasing dramas, I wonder if anyone has noticed that sliding the screen or dragging the slider bar in these software can directly control the volume without pressing the volume button on the phone's hardware.

At some point, the program needs to manage the system volume or mute the system directly, which is achieved with the help of AudioManager provided by Android

First call the getSysytemService method to obtain the audio manager of the system, and then call the AudioManager method to control the audio of the mobile phone.

Before giving an example, you need to understand the general usage of AudioManager

AdjustStreamVolume (int streamType,int direction,int flag): call the specified type of sound on the phone. The parameters of streamType are as follows:

STREAM_ALARM: the sound of cell phone alarm

The sound of STREAM_DTMF:DTMF tones

STREAM_MUSIC: the sound of mobile music

STREAM_NOTIFICATION: the sound of the system prompt

STREAM_RING: the sound of the phone ringing

...

Next, let's take a look at specific examples.

Create a few button in xml, and then open MainActivity

First, you need to create instances of each button in onCreate and call the getSysytemService method to get the audio manager of the system.

Public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); aManager = (AudioManager) getSystemService (Service.AUDIO_SERVICE); play = (Button) findViewById (R.id.play); up = (Button) findViewById (R.id.up); down = (Button) findViewById (R.id.down); mute = (ToggleButton) findViewById (R.id.mute);}

Continue to play music in onCreate

Play.setOnClickListener (new OnClickListener () {@ Override public void onClick (View source) {MediaPlayer mPlayer = MediaPlayer.create (MainActivity.this, R.raw.earth); mPlayer.setLooping (true); mPlayer.start ();}})

Continue to increase the volume in onCreate

Up.setOnClickListener (new OnClickListener () {@ Override public void onClick (View source) {aManager.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);}})

Continue to reduce the volume in onCreate

Down.setOnClickListener (new OnClickListener () {@ Override public void onClick (View source) {aManager.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);}})

Finally, mute is realized in onCreat.

Mute.setOnCheckedChangeListener (new OnCheckedChangeListener () {@ Override public void onCheckedChanged (CompoundButton source, boolean isChecked) {/ / specify to adjust the audio of the music, and determine whether you need to mute aManager.setStreamMute (AudioManager.STREAM_MUSIC, isChecked) according to isChecked;}}) Thank you for your reading, the above is the content of "how to use AudioManager", after the study of this article, I believe you have a deeper understanding of how to use AudioManager, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report