When we use the Android phone, we often use the voice to increase and reduce the operation, in the setting of the situation mode is often used to mute and vibration operation, which is often controlled by Audiomanager .
today, let's take a look. Audiomanager the use of.
The first thing to do is to get this service, in front of which we've learned how to get a system service
Audiomanager audio = (Audiomanager) super.getsystemservice (Context.audio_service);
Then use the methods in this class to manipulate the sound components, followed by an example to illustrate
Xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <imagebutton android:id=" @+id/imagebutton3 "android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:layout_aligntop= "@+id/imagebutton4" android:layout_toLeftOf = "@+id/imagebutton4" android:src= "@drawable/voiceup"/> <imagebutton android:id= "@+id/imagebutton1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentl Eft= "true" android:layout_aligntop= "@+id/imagEButton2 "android:src=" @drawable/music "/> <imagebutton android:id=" @+id/imagebutton2 "Andro Id:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_aligntop= "@+id/imageButto N5 "android:layout_torightof=" @+id/imagebutton1 "android:src=" @drawable/novoice "/> <imagebutton Android:id= "@+id/imagebutton4" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentright= "true" android:layout_alignparenttop= "true" android:layout_margintop = "18DP" android:src= "@drawable/voicedown"/> <imagebutton android:id= "@+id/imagebutton5" and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_alignbottom= "@+id/image Button3 "android:layout_aligntop=" @+id/imagebutton3 "android:layout_toleftof=" @+id/imagebutton3 "Android Oid:layout_torightof= "@+Id/imagebutton2 "android:src=" @drawable/vibrate "/></relativelayout>
Java files
<span style= "FONT-SIZE:18PX;" >package Com.example.audiomanager;import Java.io.ioexception;import Android.app.activity;import Android.content.context;import Android.media.audiomanager;import Android.media.mediaplayer;import Android.os.bundle;import Android.view.view;import Android.widget.imagebutton;import Android.widget.Toast;public Class Mainactivity extends Activity {private ImageButton Voiceon, Voiceoff, Voiceup, Voicedown, Vibate;private Audiomanag ER audio; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Voiceon = (ImageButton) This.findviewbyid (r.id.imagebutton1); voiceOff = ( ImageButton) This.findviewbyid (r.id.imagebutton2); voiceup = (ImageButton) This.findviewbyid (R.id.imagebutton3); Voicedown = (ImageButton) This.findviewbyid (r.id.imagebutton4); vibate = (ImageButton) This.findviewbyid ( R.ID.IMAGEBUTTON5);//Create MediaPlayer object and set playback resource MediaPlayer media = mediaplayer.create (Mainactivity.this, R.raW.fukua); try {//, Ready to play Media.prepare ();} catch (IllegalStateException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Enter the software to automatically play Music Media.start ();//Get Audiomanager object AUDIO = (Audiomanager) super.getsystemservice (Context.audio_service) The Listener Event Voiceon.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO) set for each button Auto-generated method StubMainActivity.this.audio.setRingerMode (Audiomanager.ringer_mode_normal); Toast.maketext (Mainactivity.this, "phone volume on", Toast.length_short). Show ();}); Voiceoff.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method StubMainActivity.this.audio.setRingerMode (Audiomanager.ringer_mode_silent); Toast.maketext (Mainactivity.this, "Phone mute", toast.length_short). Show ();}); Voiceup.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated MethOD stubMainActivity.this.audio.adjustVolume (audiomanager.adjust_raise,0); Toast.maketext (Mainactivity.this, "phone volume increase", toast.length_short). Show ();}); Voicedown.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method StubMainActivity.this.audio.adjustVolume (audiomanager.adjust_lower,0); Toast.maketext (Mainactivity.this, "Phone volume Reduction", Toast.length_short). Show ();}); Vibate.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method StubMainActivity.this.audio.setRingerMode (Audiomanager.ringer_mode_vibrate); Toast.maketext (mainactivity.this, "cell phone is vibration mode", Toast.length_short). Show ();}});}} </span>
Click on a few buttons on the screen to achieve the corresponding operation, you can learn from these sections of the things themselves to achieve a simple music player.
Next forecast: Use of Viewpaper components
Learn Android<audiomanager sound editor from scratch. 50 .>