Android developed to control the phone audio

Source: Internet
Author: User

This example plays a piece of music through MediaPlayer and controls the phone audio through Audiomanager, and the detailed explanation about Audiomanager can be referenced:Android Development Audiomanager (audio Manager)

Program run:

Instance code:

Package Com.jph.audiomanagerdemo;import Android.media.audiomanager;import Android.media.mediaplayer;import Android.os.bundle;import Android.app.activity;import Android.app.service;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.compoundbutton;import Android.widget.compoundbutton.oncheckedchangelistener;import android.widget.togglebutton;/** * DESCRIBE:</BR  > * Control Data Audio * This example plays a piece of music via MediaPlayer and * controls phone audio via Audiomanager * */public class Audiomanagerdemo extends Activity {Button Btnplay,btnup,btndown; ToggleButton Btnmute; Audiomanager Amanager; MediaPlayer mPlayer; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (R.layout.main); btndown= (Button) Findviewbyid (R.id.btndown); btnplay= (button) Findviewbyid ( R.id.btnplay); btnup= (Button) Findviewbyid (r.id.btnup); btnmute= (ToggleButton) Findviewbyid (R.id.btnmute);// Initialize audiomanageramanager= (Audiomanager) Getsystemservice (ServIce. Audio_service) Onclicklistener listener=new Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO Auto-generated Method Stubswitch (V.getid ()) {case r.id.btnplay://initialize MediaPlayer prepare to play music mplayer=mediaplayer.create ( Audiomanagerdemo.this, R.raw.music);//Set the music loop to play mplayer.setlooping (true);//Play Mplayer.start (); Break;case R.id.btnup ://Specifies that the audio to adjust the music is increased and the volume graph Amanager.adjuststreamvolume (audiomanager.stream_music,audiomanager.adjust_raise, AUDIOMANAGER.FLAG_SHOW_UI) Break;case r.id.btndown://Specifies that the audio to adjust music is reduced and the volume graph is displayed Amanager.adjuststreamvolume ( Audiomanager.stream_music,audiomanager.adjust_lower, audiomanager.flag_show_ui); break;default:break;}}};/ /Add Listener for button btndown.setonclicklistener (listener); Btnplay.setonclicklistener (listener); Btnup.setonclicklistener ( listener); Btnmute.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged ( Compoundbutton Buttonview, Boolean isChecked) {//TODO auto-generated method stub//Set Music Mute amanager.setstreammute ( Audiomanager.strEam_music, isChecked);}});}} 
Main.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 "> <Butto        n android:id= "@+id/btnplay" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"        Android:layout_centerhorizontal= "true" android:text= "Play"/> <button android:id= "@+id/btnup" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerinparent = "true" android:layout_below= "@+id/btnplay" android:text= "Increase volume"/> <button android:id= "@+id/b Tndown "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_cente Rinparent= "true" android:layout_below= "@+id/btnup" android:text= "Decrease Volume"/> <togglebutton android:id= " @+id/btnmute "Android:layout_width=" WRAp_content "android:layout_height=" Wrap_content "android:layout_centerinparent=" true "Android:layout_ below= "@+id/btndown" android:texton= "Turn off mute" android:textoff= "Turn on mute"/></relativelayout>




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.