System services provided by Android-AudioManager (audio manager) and android File Manager
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:
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: id = "@ + id/LinearLayout1" android: layout_width = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical" tools: context = "com. jay. example. audiomanagerdemo. mainActivity "> <Button android: id =" @ + id/btnstart "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "play"/> <Button android: enabled = "false" android: id = "@ + id/btnstop" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "stop"/> <Button android: id = "@ + id/btnhigher" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <Button android: id = "@ + id/btnlower" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = ""/> <Button android: id = "@ + id/btnquite" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "mute"/> </LinearLayout>
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!
<Uses-permission android: name = "android. permission. VIBRATE"/>
Download the demo in this section: Use the demo to download AudioManager.
How does realtek hd audio manager set sound in the headset?
Click the small horn in the lower-right corner, find the Front pink in option in the property bar, and tick it. debugging is OK.
How can I retrieve the icon of the audio manager?
Hello, I'm glad to answer your question:
There is no sound in the computer, such as no sound when the computer is turned on, the small speakers in the taskbar are gone, often making the cainiao unable to do anything, do not know how to start. In practice, everyone will encounter similar problems. I used my computer three times. After repeated exploration, I finally found several solutions. Now I am offering it for my reference. First of all, the absence of sound on the computer seems simple. In fact, sometimes the problem is complicated, involving both hardware and software. It is also common that sound cards do not sound and small speakers disappear due to sudden crashes. Therefore, we must first identify the cause, whether it is a software fault or a hardware fault, and then properly resolve the problem. Never let the cause be unknown. if the cause is not clear, the system will be paralyzed and the trouble will increase. 1. Check whether the sound card, connection line, and speaker are properly connected. If a virus exists, run the anti-virus software to completely eliminate the virus. 2. Open Control Panel-add new hardware-Next. After the search is complete, click "no, I have not added this hardware" and click "Next. In the displayed dialog box, select "click Finish, close the computer that does not exist, and restart to add new hardware ". Note that it refers to restarting after shutdown, rather than restarting. Generally, the problem can be solved after the instance is restarted. 3. Right-click "my computer"-"properties"-"hardware"-"Device Manager" to enable "sound, video, and game controller, check whether there is a yellow "in front of it. ". If yes, uninstall it first, then put it into the CD drive disk, and reinstall the driver of the device. 4. If "Device Manager"-"sound, video, and game controller" means no sound card hardware, you can find your sound card, including integrated sound card. The model must be accurate, after finding the sound card, place it on the system drive disk and install the sound card driver. 5. When there is sound but there is no small horn, you don't need to panic. Just open the control panel-sound and video device-volume, and place a check mark before "put the volume icon in the taskbar, after the restart, there will be a small speaker in the taskbar. 6. When there is no sound and no small speakers, open the control panel-add and delete programs-add and delete Windows Components, place them on the CD drive disk, and place a check mark before "accessories and Tools, click "Next". The system will prompt you to install the new hardware. Follow the prompts to install it. 7. If the above steps cannot solve the problem, you have to re-install the system and the driver, and all the problems will be solved, but the premise is that the hardware is in good condition and the software is not in conflict. 8. When the sound card driver is installed, the "cannot find the AC chip" prompt may be computer poisoning, or the sound card plug-and-play is loose, leading to poor contact, the possible reason is that the audio card driver sequence is accidentally deleted due to illegal operations or internal software conflicts. This should be solved step by step for different reasons. 9. Simply ignore it, You can pat the shell of the computer body gently. The laptop can be slightly larger than usual when the lid is covered, so that some loose parts can be restored, after shutdown, wait for a while before re-opening the machine. Sometimes the sound problem will be restored. Try it.