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