Android之音量调节
main.xml布局文件
<?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="播放音乐" /> <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="静音" android:textOff="正常" android:checked="true" android:layout_gravity="center_vertical" /> <Button android:id="@+id/btnUpper" android:text="增大音量" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btnLower" android:text="减小音量" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
AudioActivity类:
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; //声频 private AudioManager audioManager=null; //音频 @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); //设置是否静音 } }); } View.OnClickListener listener=new View.OnClickListener(){ public void onClick(View v) { @SuppressWarnings("unused") Button btn=(Button)v; switch (v.getId()) { case R.id.btnPlay://播放音乐 mediaPlayer=MediaPlayer.create(AudioActivity.this, R.raw.music); mediaPlayer.setLooping(true);//设置循环播放 mediaPlayer.start();//播放声音 break; case R.id.btnUpper://增多音量 //adjustStreamVolume: 调整指定声音类型的音量 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); //调高声音 break; case R.id.btnLower://减少音量 //第一个参数:声音类型 //第二个参数:调整音量的方向 //第三个参数:可选的标志位 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);//调低声音 break; } } }; }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28