System services provided by Android-AudioManager)
System services provided by Android-AudioManager)
---- Reprinted with the source: coder-pig
Introduction to AudioManager and common methods:
Simple Example:
Use Mediaplayer to play music and AudioManager to adjust the volume and mute:
Here, we need to put the audio file to be played in the raw folder under res. This folder does not exist by default and needs to be created by ourselves!
The native resource is not converted into a binary file during packaging and compilation !!!
Let's take a look:
It is to play the music, and then when the volume is increased, you can see the slider, and then turn the volume down to set the sound, do not display the slide:
This is determined by the third parameter of the adjustStreamVolume () method!
Now let's start writing the Code:
Simple layout: activity_main.xml:
Then MainActivity. java:
Package com. jay. example. audiomanagerdemo; import android. app. activity; import android. app. service; import android. media. audioManager; import android. media. mediaPlayer; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; public class MainActivity extends Activity implements OnClickListener {private MediaPlayer mePlayer; private AudioManager aManager; private Button btnstart; private Button btnstop; private Button btnhigher; private Button btnlower; private Button btnquite; // define a flag to indicate whether the mute button is clicked. private int flag = 1; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); btnstart = (Button) findViewById (R. id. btnstart); btnstop = (Button) findViewById (R. id. btnstop); btnhigher = (Button) findViewById (R. id. btnhigher); btnlower = (Button) findViewById (R. id. btnlower); btnquite = (Button) findViewById (R. id. btnquite); // obtain the audio object aManager = (AudioManager) getSystemService (Service. AUDIO_SERVICE); // initialize the mediaplayer object. Here, the mp3 resource mePlayer = MediaPlayer in the raw file is played. create (MainActivity. this, R. raw. one); // set loop playback: mePlayer. setLooping (true); btnstart. setOnClickListener (this); btnstop. setOnClickListener (this); btnhigher. setOnClickListener (this); btnlower. setOnClickListener (this); btnquite. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btnstart: btnstop. setEnabled (true); mePlayer. start (); btnstart. setEnabled (false); break; case R. id. btnstop: btnstart. setEnabled (true); mePlayer. pause (); btnstop. setEnabled (false); break; case R. id. btnhigher: // adjust the audio of the music, increase the volume, and display the volume chart to indicate aManager. adjustStreamVolume (AudioManager. STREAM_MUSIC, AudioManager. ADJUST_RAISE, AudioManager. FLAG_SHOW_UI); break; case R. id. btnlower: // specify to adjust the audio of the music, reduce the volume, only the sound, do not display the graphics bar aManager. adjustStreamVolume (AudioManager. STREAM_MUSIC, AudioManager. ADJUST_LOWER, AudioManager. FLAG_PLAY_SOUND); break; case R. id. btnquite: // specify the audio for music adjustment. Determine whether to mute the flag * =-1 based on isChecked; if (flag =-1) {aManager. setStreamMute (AudioManager. STREAM_MUSIC, true); btnquite. setText ("cancel mute");} else {btnquite. setText ("mute"); aManager. setStreamMute (AudioManager. STREAM_MUSIC, false) ;}break ;}}}
The Code is also very simple, so there is no more explanation here!
NOTE: IfThe third parameter of adjustStreamVolume () You set Vibrator, which must be in AndroidManifest. xml
To add this permission!
Download the demo in this section: Use the demo to download AudioManager.