package com.example.test2; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.app.Activity; import android.app.Service; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ToggleButton; public class MainActivity 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.activity_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(MainActivity.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; } } }; }
<?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>
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/13401.html