Android-based volume adjustment

Source: Internet
Author: User

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_content"
Android: text = "playing music"/>
<LinearLayout android: orientation = "horizontal"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal">
<ToggleButton android: id = "@ + id/tbMute"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textOn = "mute"
Android: textOff = "normal"
Android: checked = "true"
Android: layout_gravity = "center_vertical"/>
<Button android: id = "@ + id/btnUpper"
Android: text = "increase the volume"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button android: id = "@ + id/btnLower"
Android: text = "Reducing volume"
Android: layout_width = "wrap_content"
Android: layout_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 Frequency
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 buttonView, boolean isChecked ){
AudioManager. setStreamMute (AudioManager. STREAM_MUSIC ,! IsChecked); // sets whether to mute
}
});
}

View. OnClickListener listener = new View. OnClickListener (){
Public void onClick (View v ){
@ SuppressWarnings ("unused ")
Button btn = (Button) v;
Switch (v. getId ()){
Case R. id. btnPlay: // play music
MediaPlayer = MediaPlayer. create (AudioActivity. this, R. raw. music );
MediaPlayer. setLooping (true); // sets loop playback.
MediaPlayer. start (); // playback sound
Break;
Case R. id. btnUpper: // increase the volume
// AdjustStreamVolume: adjust the volume of the specified sound type
AudioManager. adjustStreamVolume (AudioManager. STREAM_MUSIC,
AudioManager. ADJUST_RAISE,
AudioManager. FLAG_SHOW_UI); // raise the sound
Break;
Case R. id. btnLower: // reduce the volume
// The first parameter: Sound type
// Second parameter: adjust the volume direction
// The third parameter: Optional flag
AudioManager. adjustStreamVolume (AudioManager. STREAM_MUSIC,
AudioManager. ADJUST_LOWER,
AudioManager. FLAG_SHOW_UI); // lower the sound
Break;
}
}
};

}

Running result

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.