How to achieve volume control on Android _android

Source: Internet
Author: User

An example of this article describes how Android achieves volume tuning. Share to everyone for your reference. Specifically as follows:

Main.xml Layout file:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " > <button android:id= "@+id/btnplay" android:layout_width= fill_parent "android:layout_height=" Wrap_conten T "android:text=" play Music "/> <linearlayout android:orientation=" Horizontal "android:layout_width=" Wrap_conte NT "android:layout_height=" Wrap_content "android:layout_gravity=" Center_horizontal "> <togglebutton Andro Id:id= "@+id/tbmute" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Android:tex
    Ton= "Mute" android:textoff= "normal" android:checked= "true" android:layout_gravity= "center_vertical"/> <button android:id= "@+id/btnupper" android:text= "Increase volume" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content"/>; Button android:id= "@+id/btnlower" android:text= "Reduce volume" android:layout_width= "Wrap_content" android:layou t_height= "Wrap_content"/> </LinearLayout> </LinearLayout>

Audioactivity class:

Package com.ljq.activity;
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.widget.Button;
Import Android.widget.CompoundButton;
Import Android.widget.ToggleButton;
Import Android.widget.CompoundButton.OnCheckedChangeListener;
  public class Audioactivity extends activity {private Button btnplay=null, Btnupper=null, Btnlower=null;
  Private ToggleButton Tbmute=null; Private MediaPlayer Mediaplayer=null; Audio private Audiomanager audiomanager=null;
    Audio @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Audiomanager= (Audiomanager) Getsystemservice (Service.audio_service);
    Btnplay= (Button) Findviewbyid (R.id.btnplay);
    Btnupper= (Button) Findviewbyid (r.id.btnupper);
    Btnlower= (Button) Findviewbyid (r.id.btnlower); Btnplay.setonclicklistener (ListEner);
    Btnupper.setonclicklistener (listener);
    Btnlower.setonclicklistener (listener);
    Tbmute= (ToggleButton) Findviewbyid (R.id.tbmute); Tbmute.setoncheckedchangelistener (New Oncheckedchangelistener () {public void oncheckedchanged (Compoundbutton button
    View, Boolean ischecked) {Audiomanager.setstreammute (audiomanager.stream_music,!ischecked);//Set Mute}
  }); } view.onclicklistener listener=new View.onclicklistener () {public void OnClick (View v) {@SuppressWarnings ("U
      nused ") button btn= (button) v; Switch (V.getid ()) {case r.id.btnplay://play Music mediaplayer=mediaplayer.create (Audioactivity.this, R.raw.music
        );
      Mediaplayer.setlooping (TRUE);/set loop play Mediaplayer.start ();//play sound break; Case r.id.btnupper://Increase Volume//adjuststreamvolume: Adjusts the volume audiomanager.adjuststreamvolume of the specified sound type (AUDIOMANAGER.S Tream_music, Audiomanager.adjust_raise, Audiomanager.flag_shOW_UI);
      Increase sound break; Case r.id.btnlower://Decrease Volume//First parameter: Sound type//second parameter: Adjust volume direction//third parameter: Optional flag bit audiomanager.adjusts Treamvolume (Audiomanager.stream_music, Audiomanager.adjust_lower, audiomanager.flag_show_ui);
      Low sound break;
}
    }
  }; }

Run Result:

I hope this article will help you with your Android program.

Related Article

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.