android--Sound Service (Audiomanager)

Source: Internet
Author: User

The Main.xml code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <imagebutton android:id= "@+id/voiceon" android:layout_width= "Wrap_content" android:layout_height= " Wrap_content "android:src=" @drawable/voice_on "/> <imagebutton android:id=" @+id/voiceoff "an Droid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/voice_off"/&gt      ; <imagebutton android:id= "@+id/voicevibrate" android:layout_width= "Wrap_content" Android:layout_hei ght= "Wrap_content" android:src= "@drawable/voice_vibrate"/> <imagebutton android:id= "@+id/voicelo Wer "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:src=" @drawable/     Voice_lower "/>   <imagebutton android:id= "@+id/voiceraise" android:layout_width= "Wrap_content" android:layout_he ight= "Wrap_content" android:src= "@drawable/voice_raise"/></linearlayout>
The activity program is as follows:

Package Org.lxh.demo;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.view.view.onclicklistener;import Android.widget.imagebutton;import Android.widget.toast;public Class Myaudiomanagerdemo extends Activity {private ImageButton Voiceon = null;p rivate ImageButton voiceoff = null;p Rivat e ImageButton voicevibrate = null;p rivate ImageButton voicelower = null;p rivate ImageButton voiceraise = null;p rivate A Udiomanager audio = null; @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Super.setcontentview (r.layout.main); This.voiceon = (ImageButton) Super.findviewbyid (r.id.voiceon); this.voiceOff = (ImageButton) Super.findviewbyid (r.id.voiceoff); this.voicevibrate = (ImageButton) Super.findviewbyid ( r.id.voicevibrate); this.voicelower = (ImageButton) Super.findviewbyid (r.id.voicelower); this.voiceraise = (ImageButton) Super.findviewbyid (r.id.voiceraise); This.audio = (Audiomanager) Super.getsystemservice (Context.audio_service); This.voiceOn.setOnClickListener (new Voiceononclicklistenerimpl ()) ; This.voiceOff.setOnClickListener (New Voiceoffonclicklistenerimpl ()); This.voiceVibrate.setOnClickListener (new Voicevibrateonclicklistenerimpl ()); This.voiceLower.setOnClickListener (new Voiceloweronclicklistenerimpl ()); This.voiceRaise.setOnClickListener (New Voiceraiseonclicklistenerimpl ()); This.playaudio ();} private void Playaudio () {MediaPlayer media = mediaplayer.create (this, R.raw.mldn_java); media.setlooping (true);// Loop play try {media.prepare ();} catch (Exception e) {}media.start ();} Private class Voiceononclicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) { MyAudioManagerDemo.this.audio.setRingerMode (Audiomanager.ringer_mode_normal); Toast.maketext (Myaudiomanagerdemo.this, "Phone volume on! ", Toast.length_short). Show ();}} Private Class VoiceoffonclicklisTenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) { MyAudioManagerDemo.this.audio.setRingerMode (audiomanager.ringer_mode_silent); Toast.maketext (Myaudiomanagerdemo.this, "Mute the phone! ", Toast.length_short). Show ();}} Private class Voicevibrateonclicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) { MyAudioManagerDemo.this.audio.setRingerMode (audiomanager.ringer_mode_vibrate); Toast.maketext (Myaudiomanagerdemo.this, "mobile phone for vibration mode! ", Toast.length_short). Show ();}} Private class Voiceloweronclicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) { MyAudioManagerDemo.this.audio.adjustVolume (audiomanager.adjust_lower, 0); Toast.maketext (Myaudiomanagerdemo.this, "Volume down! ", Toast.length_short). Show ();}} Private class Voiceraiseonclicklistenerimpl implements Onclicklistener {@Overridepublic void OnClick (View v) { MyAudioManagerDemo.this.audio.adjustVolume (audiomanager.adjust_raise, 0); Toast.maketext (Myaudiomanagerdemo.this, "volume increased! ", Toast.length_short). Show ();}}} 


The results are as follows:


android--Sound Service (Audiomanager)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.